<html>
    <head>
      <base href="http://bugzilla.gdcproject.org/" />
    </head>
    <body>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - NRVO not implemented"
   href="http://bugzilla.gdcproject.org/show_bug.cgi?id=52#c6">Comment # 6</a>
              on <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - NRVO not implemented"
   href="http://bugzilla.gdcproject.org/show_bug.cgi?id=52">bug 52</a>
              from <span class="vcard"><a class="email" href="mailto:johannespfau@gmail.com" title="Johannes Pfau <johannespfau@gmail.com>"> <span class="fn">Johannes Pfau</span></a>
</span></b>
        <pre>One more simplified example:

<a href="http://dpaste.dzfl.pl/d5506e00d3be">http://dpaste.dzfl.pl/d5506e00d3be</a>
----
import std.stdio;

struct S
{
  int a;
  int b;
  // Without this this small struct is returned in a register and even with DMD
  // the address is changing!
  int[64] c;
}

S nrvo()
{
  S s;
  s.a = 42;
  writeln(&s);
  return s;
}

void main()
{
  auto s = nrvo();
  writeln(&s);
}
----

Addresses are different with gdc, the same with dmd. However, as noted earlier
there's nothing in the spec that tells us what's the correct behavior. The fact
that even DMD does not prevent an address change if the struct is small enough
to be returned in registers and that this whole 'feature' relies on ABI details
probably shows that this is an anti-pattern.

We probably need an explicit way in the language to tell the compiler to
allocate space in the caller and then pass a reference to the callee:

void nrvo(@caller ref S s)
{
    s.a = 42;
}

auto s = nrvo();
//the same as
S s;
nrvo(s);</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are watching all bug changes.</li>
      </ul>
    </body>
</html>