delegates and temporary struct

1100110 0b1100110 at gmail.com
Thu May 16 19:35:45 PDT 2013


On 05/16/2013 09:21 PM, Maxim Fomin wrote:
> On Thursday, 16 May 2013 at 22:53:56 UTC, Jack Applegame wrote:
>> 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"
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=9352

I get an incorrect but consistent number using dmd -m32 test.d and the
correct number using dmd -m64 test.d on Linux.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 900 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20130516/5c3c3179/attachment.pgp>


More information about the Digitalmars-d-learn mailing list