Changing elements during foreach

Krzysztof Ciebiera ciebie at mimuw.edu.pl
Mon Oct 21 03:31:49 PDT 2013


Is the following compiler behavior consistent with language 
specification?

import std.stdio;

void main()
{
     int a[][] = [[1,2,3]];
     foreach(x; a)
     {
         x[0] = 0;
         x ~= 4;
     }
     writeln(a);
}

I understand why thw program could output [1,2,3] (like in C++ 
without &) or [0,2,3,4] (python, C++ ref). But [0,2,3]? It was 
unpleasant surprise.


More information about the Digitalmars-d-learn mailing list