views:

128

answers:

1

In C++0x, methods can be overloaded on whether or not the expression that denotes the object on which the method is called is an lvalue or an rvalue. If I return *this from a method called via an rvalue, do I need to explicitly move from *this or not?

Foo Foo::method() &&
{
    return std::move(*this);   // Is this move required or not?
}

Unfortunately, I can't simply test this on my compiler since g++ does not support this feature yet :(

+1  A: 

The function returns a Foo by value, why would you need to cast *this to Foo&&?

Did you mean to return a Foo&&?

If that is the case, then the answer is still no, because *this is a Foo&& anyway because of the && ref-qualifier:

C++0x FCD § 13.3.1
4. For non-static member functions, the type of the implicit object parameter is
— “lvalue reference to cv X” for functions declared without a ref-qualifier or with the & ref-qualifier
— “rvalue reference to cv X” for functions declared with the && ref-qualifier

Peter Alexander
FredOverflow
I added the quote from the standard.
Peter Alexander