DMD Bug or not? foreach over struct range

Jonathan M Davis jmdavisProg at gmx.com
Wed May 16 00:20:39 PDT 2012


On Wednesday, May 16, 2012 09:15:06 bearophile wrote:
> Nick Sabalausky:
> > It seems that foreach is iterating over an implicit copy of
> > 'foo' instead of
> > 'foo' itself. Is this correct behavior, or a DMD bug?
> 
> This program prints "[11, 21, 31]" so for fixed-size arrays (that
> are values as much as structs) it doesn't perform a copy:
> 
> 
> import std.stdio;
> void main() {
>       int[3] a = [10, 20, 30];
>       foreach (ref x; a)
>           x++;
>       writeln(a);
> }

It probably slices the array, which would mean that it was operating on a 
dynamic array rather than a static one. But it could just generate different 
code for static arrays.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list