[Bug 52] NRVO not implemented

gdc-bugzilla at gdcproject.org gdc-bugzilla at gdcproject.org
Tue Feb 25 01:03:33 PST 2014


http://bugzilla.gdcproject.org/show_bug.cgi?id=52

--- Comment #6 from Johannes Pfau <johannespfau at gmail.com> ---
One more simplified example:

http://dpaste.dzfl.pl/d5506e00d3be
----
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);

-- 
You are receiving this mail because:
You are watching all bug changes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d.gnu/attachments/20140225/0aaf9fb8/attachment-0001.html>


More information about the D.gnu mailing list