const ref and rvalues

Timon Gehr timon.gehr at gmx.ch
Thu Feb 16 09:40:39 PST 2012


On 02/16/2012 05:36 PM, Jonathan M Davis wrote:
> On Thursday, February 16, 2012 16:12:51 Timon Gehr wrote:
>> On 02/16/2012 11:07 AM, Don Clugston wrote:
>>> On 16/02/12 06:38, Martin Nowak wrote:
>
>>>> C++'s 'const&' is very easy to get wrong.
>>>
>>> Yes, but D still hasn't got it right.
>>> As far as I can tell, "const ref" in a parameter list is ALWAYS a bug.
>>
>> Why? It is perfectly reasonable to pass a large struct by const ref.
>
> Because then you _have_ to have a variable to call the function - except for
> the bizarre situation that struct literals have where they're considered
> lvalues (very bad idea IMHO). I think that from the perspective of most
> programmers, the fact that const ref doesn't take rvalues is a major negative,
> even if Andrei is ultimately right (though I don't even remember what his
> reasoning is - I'm not sure that I've ever understod it).
>
> The current solution is to use auto ref, but that only works with templates (I
> don't know if that's the plan or just a bug of the current implementation).
> So, the current situation is definitely broken.
>
> If auto ref is changed to work with non-templated functions,  then _why_ would
> you ever use const ref? You end up with a function that will only work with
> lvalues but where you don't actually intend to change the original. So, you
> probably end up with functions being either auto ref (or const auto ref) or
> ref with no const ref, making const ref ultimately rather pointless.
>
> - Jonathan M Davis

Ok. Is there a ticket for implementing auto ref for non-templated 
functions already?


More information about the Digitalmars-d mailing list