DMD Bug or not? foreach over struct range

Steven Schveighoffer schveiguy at yahoo.com
Thu May 17 07:59:22 PDT 2012


On Wed, 16 May 2012 03:20:39 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> 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.

foreach does *not* use range primitives for arrays, they are special.

-Steve


More information about the Digitalmars-d-learn mailing list