structs are now lvalues - what is with "auto ref"?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Dec 24 10:45:10 PST 2012


On 12/24/12 1:20 PM, Jonathan M Davis wrote:
> On Monday, December 24, 2012 22:11:58 Dmitry Olshansky wrote:
>> 12/24/2012 9:57 PM, Jonathan M Davis пишет:
>>> On Monday, December 24, 2012 12:37:54 Andrei Alexandrescu wrote:
>>>> An important smoking gun is C++'s min(), which is allows writing unsafe
>>>> code without casts.
>>>>
>>>> const int&  haveANiceDay = min(4, 5);
>>>
>>> But what does that have to do with const&  for function parameters?
>>
>> It relates directly as one may then pass the result of min to fun(const
>> ref T);
>
> But the temporary has to be kept in existence for the duration of the
> statement, so I don't see how that's a problem.

The problem occurs of course once haveANiceDay is actually used.

> The temporary will last as
> long as the function call does. Worst case, the function returns a const ref
> to its parameter, but either that ref is used as part of the same statement,
> and so the temporary continues to exist and there are no problems, or it's
> copied when it's assigned to a variable, and there are no problems. It's the
> fact that you can have a const&  local variable that breaks things, and D
> doesn't allow that.

The very binding of rvalues to const ref would allow that breakage. We 
can't allow that to happen.


Andrei



More information about the Digitalmars-d-learn mailing list