Why does foreach allow ref element for non-ref front() of std.range.enumerate()

Boris-Barboris ismailsiege at gmail.com
Sun Jan 26 11:09:14 UTC 2020


import std;
import std.range;
void main()
{
     int[] a = [3, 5, 7];
     foreach (i, ref ae; a.enumerate)
     {
      	writeln(i, " ", ae);
         ae = 6;
     }
     writeln(a);
     assert(a[].equal([6, 6, 6]));  // fails, a is in initial state
}


Why does the compiler allow such 'ref ae' loop, if the actual 
value is not a reference? Or it is a reference to hidden stack 
variable that was copied by enumerate from a during front()?


More information about the Digitalmars-d-learn mailing list