Const ref and rvalues again...

monarch_dodra monarchdodra at gmail.com
Wed Oct 17 23:11:21 PDT 2012


On Thursday, 18 October 2012 at 04:30:17 UTC, Jonathan M Davis 
wrote:
> On Thursday, October 18, 2012 06:24:08 jerro wrote:
>> What would be the problem with const ref taking rvalues?
>
> Read the thread that I already linked to:
>
> http://forum.dlang.org/thread/4F84D6DD.5090405@digitalmars.com
>
> - Jonathan M Davis

I read the thread, and not a single one of the "problematic 
cases" are actually valid C++.

Yes: the faulty MSVC has taught people to do retarded things, or 
be afraid of things that were illegal to begin with (in 
particular, pass an rvalue to a ref, WHICH IS ILLEGAL IN C++), 
such as "increment(5)".

There is actually nothing wrong with creating a temporary when 
something is bound to a const ref, provided the compiler follows 
the rules:

*Only LValues with an EXACT type match may be passed to a 
reference.
*In regards to *const* references, RValues may be copied in a 
temporary, and that temporary bound the the ref.

I'm not saying we particularly *need* this in D (C++ has a "by 
ref" paradigm that makes it more important, but D *rarelly* ever 
passes by const ref).

But if the compiler respects the above two rules (which it 
should), then RValue to const ref is both perfectly doable and 
safe (as safe as refs get anyways).


More information about the Digitalmars-d mailing list