R-values and const ref

Steven Schveighoffer schveiguy at yahoo.com
Thu Sep 9 04:32:18 PDT 2010


On Thu, 09 Sep 2010 06:08:14 -0400, Eldar Insafutdinov  
<e.insafutdinov at gmail.com> wrote:

> A while back dmd stopped allowing rvalues as ref function arguments. I
> entirely understand why it was done, but the same restriction applies to  
> the
> ref const function parameters. This causes a lot of pain and at the  
> moment
> makes me use pass-by-value parameters(most notably when using structs).  
> C++
> doesn't have any problem with const ref and rvalues. Can we please  
> restore
> this behavior for const ref?

auto ref is supposed to handle that.  However, I think it only works on  
templates.

The thing is, when you pass an rvalue by reference, it's more expensive  
than passing it by value.  If passed by value, since it's an rvalue, the  
compiler knows that it no longer is needed elsewhere, so it can just store  
it on the stack in the place it will be passing it.

I still think the design needs some work, and there are some compiler  
annoyances that can't be helped (like the compiler complaining that  
opEquals must take const ref T) that make it impossible to write correct  
code.

-Steve


More information about the Digitalmars-d mailing list