auto ref is on the docket

via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 22 06:10:38 PDT 2015


On Monday, 22 June 2015 at 10:04:28 UTC, Marc Schütz wrote:
> On Monday, 22 June 2015 at 06:38:57 UTC, Andrei Alexandrescu 
> wrote:
>> On 6/21/15 11:31 PM, Andrei Alexandrescu wrote:
>>> On 6/21/15 10:25 PM, Walter Bright wrote:
>>>> The idea is that fun(5) would be lowered to:
>>>>
>>>>     auto tmp = 5;
>>>>     fun(tmp);
>>>
>>> I don't think that lowering is recommended - it prolongs the 
>>> lifetime of
>>> the temporary through the end of the caller. But that may be 
>>> actually a
>>> good thing.
>>
>> On second thought - Walter's lowering, which makes the rvalue 
>> last more than strictly necessary, may be the most flexible of 
>> all at the cost of more resource consumption (for types that 
>> define destructors). -- Andrei
>
> Why would that be desirable? Resource consumption is the least 
> of the problems. It is simply surprising, inconsistent with the 
> lifetime of other temporaries, and it is unnecessary. Just 
> lower it to:
>
>     {
>         auto tmp = 5;
>         fun(tmp);
>     }

I see now that Andrei already wrote in the PR "creating a named 
temporary in a scope immediately enclosing the call". I would 
like to add that AFAIK other temporaries live until the end of 
the entire statement they appear in, not just for the duration of 
the call. In any case, the temporaries involved here should not 
behave different from normal temporaries; in particular, they 
should have the same lifetime as with pass-by-value.


More information about the Digitalmars-d mailing list