January 26, 2020 Why does foreach allow ref element for non-ref front() of std.range.enumerate() | ||||
---|---|---|---|---|
| ||||
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()? |
Copyright © 1999-2021 by the D Language Foundation