auto ref is on the docket

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 21 23:43:59 PDT 2015


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

I would think in the general case, the variable would need to 
stick around until the statement was completed in order to avoid 
problems with it going away prematurely, though maybe the recent 
changes with return as an attribute fix that problem. But as long 
as its legal for the function to return an auto ref argument via 
ref or auto ref, keeping the variable around for the duration of 
the call - or even just the expression - rather than the 
statement would risk using the variable after it was destroyed.

- Jonathan M Davis


More information about the Digitalmars-d mailing list