delegates and temporary struct
Jack Applegame
japplegame at gmail.com
Thu May 16 15:53:55 PDT 2013
Look at this code:
>import std.stdio;
>
>class Foo {
> int value = 123456;
>}
>
>struct Bar {
> this(Foo f) { foo = f; }
> @property auto lambda() {
> return (){ writefln("value = %s", foo.value); };
> }
> Foo foo;
>}
>auto getLambda(Foo f) {
> return Bar(f).lambda; // lambda closure points to temporary
> Bar on the stack
>}
>void main() {
> Foo foo = new Foo;
> auto lambda = getLambda(foo);
> lambda(); // undefined behaviour? prints some number, but not
> 123456
>}
It compiles, but result is unpredictable.
Platform: Windows 7, dmd 2.062
For reproducing this behaviour it's necessary to omit -inline and
-O flags, try simple "rdmd test.d"
More information about the Digitalmars-d-learn
mailing list