DMD Bug or not? foreach over struct range

bearophile bearophileHUGS at lycos.com
Wed May 16 00:15:06 PDT 2012


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);
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list