[Issue 7130] New: NRVO Bug: Wrong Code With D'tor + Conditional Return

dsimcha dsimcha at yahoo.com
Sun Dec 18 19:26:12 PST 2011


The NG server was down when I submitted this to Bugzilla and it's a 
pretty important issue, so I'm posting it to the NG manually now:

http://d.puremagic.com/issues/show_bug.cgi?id=7130

import core.stdc.stdio;

struct S {
     this(this) {
         printf("Postblit\n");
     }

     ~this() {
         printf("D'tor\n");
     }
}

S doIt(int i) {
     S s1;
     S s2;
     printf("s1 lives at %p.\n", &s1);
     printf("s2 lives at %p.\n", &s2);
     return (i == 42) ? s1 : s2;
}

void main() {
     auto s = doIt(3);
     printf("s lives at %p.\n", &s);
}

Output:

s1 lives at 0xffc54368.
s2 lives at 0xffc54369.
D'tor
D'tor
s lives at 0xffc5437c.
D'tor

Both D'tors are called and the returned result lives at a different address
after being returned than before, as expected if not using NRVO.  On the 
other
hand, no postblit being called for whichever struct is returned, as 
expected if
using NRVO.


More information about the Digitalmars-d-bugs mailing list