Const ref and rvalues again...

martin kinke at libero.it
Wed Nov 7 09:04:08 PST 2012


On Wednesday, 7 November 2012 at 14:07:31 UTC, martin wrote:
> C++:
> void f(T& a) { // for lvalues
>     this->resource = a.resource;
>     a.resetResource();
> }
> void f(T&& a) { // for rvalues (moved)
>     this->resource = a.resource;
>     a.resetResource();
> }
>
> D:
> void f(ref T a) { // for lvalues
>     this.resource = a.resource;
>     a.resetResource();
> }
> void f(T a) { // rvalue argument is not copied, but moved
>     this.resource = a.resource;
>     a.resetResource();
> }

You could probably get away with a single-line overload, both in 
C++ and D:

C++:
void f(T& a) { // for lvalues
     // convert a to mutable rvalue reference and
     // invoke the main overload f(T&&)
     f(std::move(a));
}

D:
void f(T a) { // rvalue argument is not copied, but moved
     // the original argument is now named a (an lvalue)
     // invoke the main overload f(ref T)
     f(a);
}


More information about the Digitalmars-d mailing list