Changing the foreach key...

Sean Kelly sean at f4.ca
Mon Oct 30 09:25:39 PST 2006


Lionello Lunesu wrote:
> Is this according to the spec?
> 
> #void main(char[] args[])
> #{
> #    foreach(i,l;args[0]) {
> #        printf("%c\n",l);
> #        ++i;
> #    }
> #}
> 
> I'm changing the key (index) inside the foreach, and foreach actually 
> uses my index. The example above prints every other character.

I'd say this represents undefined behavior.  If the compiler simply 
converts the foreach to a for loop then you will see the above behavior, 
but it doesn't have to...


Sean



More information about the Digitalmars-d-learn mailing list