Copy elision by spec

monarch_dodra monarchdodra at gmail.com
Mon Nov 4 05:15:51 PST 2013


On Monday, 4 November 2013 at 11:41:04 UTC, Lars T. Kyllingstad
wrote:
> On Monday, 4 November 2013 at 07:17:12 UTC, monarch_dodra wrote:
>> http://d.puremagic.com/issues/show_bug.cgi?id=11287
>>
>> According to the conversation, NRVO is supposed to be part of 
>> the spec, and you should expect it to work.
>
> I'm not an expert, but aren't there cases where NRVO can't be 
> applied?  For example:
>
>   S foo()
>   {
>      S s1;
>      // Initialize s1
>      S s2;
>      // Initialize s2
>      if (someCondition) return s1;
>      else return s2;
>   }
>
> However, the return value can always be *moved*.

According to Andrei
(http://video.ch9.ms/sessions/gonat/2013/AndreiQuickCodeGn13.zip
slide 37), NRVO requires "All paths return same local". If not,
"assume an extra copy".

IMO "assume" means that the compiler *may* elide the copy, but
*that* would be an optimization. If all your paths return the
same named local, then NRVO should be guaranteed.

But I think the spec should formalize the behavior.


More information about the Digitalmars-d mailing list