Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++0x T operator+(const T&, T&&) pattern, still needs move?

Some time ago I was told, that the usual pattern to implement two-ary operators needs a final move in the return.

Matrix operator+(const Matrix &a, Matrix &&b) {
    b += a;
    return std::move(b);
}

But now there is the special rule that in a return the compiler may treat the return value as a temporary, and then this would not be necessary -- a simple return b would suffice.

But then again, b has a name in this function, therefore, its an LValue -- which hinders the compiler to m consider it being a temp, and the move is required.

Is this still the case in the most recent version of the C++0x Standard? We need the move to implement the above pattern?

like image 642
towi Avatar asked Aug 21 '11 19:08

towi


1 Answers

You need the explicit std::move in this example because b is not the name of a non-volatile automatic object. Reference 12.8 [class.copy] /p31/b1:

  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv- unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value
like image 82
Howard Hinnant Avatar answered Oct 12 '22 01:10

Howard Hinnant