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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With