Struct lifetime wrt function return?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 3 22:30:55 PDT 2015


On Mon, 04 May 2015 02:29:19 +0000, rsw0x wrote:

> This says "Goodbye!" exactly once, indicating(?) that S was NRVO'd which
> means the scope of s went from foo to main. However, is this a guarantee
> by the standard? Is an implementation allowed to define foo such that it
> returns by copy and calls a destructor on s, meaning "Goodbye!" would
> print out twice?

actually, you'd better avoid code that makes assumptions about struct 
copying/moving. compiler is free to do what it sees better. moreover, 
phobos can use moving too. but it also can use copy/destroy.

what i want to say is that you should write your code with structure 
copying in mind. remember that structure is value type, it can be freely 
moved and copied.

you can also explicitly disable structure copying with

  @disable this (this);

and if you concerned about code optimisation... well, NRVO is guaranteed 
in DMD, and recent GDC got it too. don't know about LDC, though, but you 
can expect that NRVO is guaranteed, yes.

if new compiler will appear (SDC, for example), i think that it will do 
NRVO too (sooner or later).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150504/44d5c577/attachment.sig>


More information about the Digitalmars-d-learn mailing list