how to correctly populate an array of dynamic closures?
kdevel
kdevel at vogtner.de
Thu Mar 29 19:02:51 UTC 2018
On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote:
> import std.stdio;
> void main () {
> int delegate () [] funs;
> funs ~= () => 0;
> funs ~= () => 1;
> foreach (i; 0..2) writeln (funs[i] ()); // 0 and 1 as expected
>
> int delegate () [] guns;
> foreach (i; 0..2) guns ~= () => i;
> foreach (i; 0..2) writeln (guns[i] ()); // 1 and 1, why?
Isn't this undefined behavior? The first loop variable named "i"
already went out of scope when the delegate is invoked.
More information about the Digitalmars-d-learn
mailing list