On Saturday, 7 February 2015 at 15:02:43 UTC, Andrei Alexandrescu wrote:
On 2/7/15 6:35 AM, Daniel Murphy wrote:
"Peter Alexander"  wrote in message
news:[email protected]...

I'm writing a blog post about why we don't need rvalue references in D. It seems that we rely on NRVO being performed, not just as an optimization, but for correct semantics (e.g. for objects without destructors or postblits). This doesn't appear to be documented anywhere.

Is it meant to be part of the spec?

NRVO isn't required for correct semantics, as structs can be moved with
bitcopy.

It is required for structs that disable postblit. -- Andrei

NRVO specifically means that a pointer to the destination object is passed to the function, and the returned object is constructed in place. The in place construction isn't required. What is required is that the local is moved.

e.g.

S foo() {
  S s;
  return s;
}
S s = foo();


With NRVO becomes:

void foo(ref S dst) {
  dst = S();
}
S s = void;
foo(s);


But this isn't necessary. Would also be valid to just do:

void foo(ref S dst) {
  S s;
  move(dst, s);  // do the memcpys
}
S s;
foo(s);


This distinction matters because NRVO cannot be performed when foo may return two different objects, but we can still move and avoid postblit.

Reply via email to