On Wednesday, 24 April 2013 at 20:53:11 UTC, Ali Çehreli wrote:
First, as the local 's' in makeS() is local, it cannot be
returned by ref. So, the 'auto ref' return type of makeS()
becomes by-value.
However, rvalues are never copied in D. The compiler
automatically moves the bits of the rvalue to the left-hand
side object, and to be correct, it also elides the destructor
execution on the rvalue. (Note that this is not the RVO and
NRVO optimization that C++ "allows". This is a language feature
in D.)
For that feature to be available at all, struct objects must
not have references to themselves; D explicitly makes it
illegal.
So, unfortunately your S is not a valid type in D because of
the following line (.ptr is added by me):
s.b = s.buf.ptr;
Ali
Thanks, this explains a lot. I was interfacing with a C library
when tracing back an error to this. I eagerly submitted a Bug
report http://d.puremagic.com/issues/show_bug.cgi?id=9985. Seems
to me that dmd doesn't do NRVO (?), see the issue.