Is NRVO part of the spec?

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Sat Feb 7 12:45:41 PST 2015


On Saturday, 7 February 2015 at 16:10:48 UTC, Andrei Alexandrescu 
wrote:
> On 2/7/15 8:02 AM, Daniel Murphy wrote:
>> "Andrei Alexandrescu"  wrote in message
>> news:mb59ej$2j7s$1 at digitalmars.com...
>>
>>> > NRVO isn't required for correct semantics, as structs can 
>>> > be moved with
>>> > bitcopy.
>>>
>>> It is required for structs that disable postblit. -- Andrei
>>
>> IIRC they only require that no copies are made.  They can 
>> still be moved.
>
> Exactly - as you just said in the other post, the spec must 
> clarify when things are guaranteed to be moved and not copied.
>
> That includes:
>
> 1. URVO: returning an rvalue does not entail a copy.
>
> 2. Last return of a function local variable does not entail a 
> copy.
>
> That's actually more than NRVO because NRVO requires the same 
> local be returned from all paths. Example:
>
> T fun(bool b) {
>   if (b) { T a; return a; }
>   T b;
>   return b;
> }
>
> This should work if T is noncopyable. It may be less efficient 
> than it could though.
>
> 3. The more complicated/ambitious cases involve the last use of 
> a value. Consider:
>
> T fun() {
>   T a;
>   T b = a;
>   return b;
> }
>
> Even though the code ostensibly makes a copy, it's the last use 
> of a so that could be a move.
>
> I think (3) could be used for optimization but it's too much of 
> a headache to put in the language definition. We do need to 
> have (1) and (2) covered.
>
>
> Andrei

Perfect. Clear and reasonable.


More information about the Digitalmars-d mailing list