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?
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
I'm not sure why this function returns by value. Shouldn't this function return a Matrix&&
like the following?
Matrix&& operator+(const Matrix &a, Matrix &&b) {
b += a;
return std::move(b);
}
This has the added advantage that x1 + x2 + x3 + ... + xn
creates at most one temporary, which is important if Matrix happens to be stack allocated (as it then gains nothing from moves).
I think the signatures should be like the following:
Matrix&& operator+(Matrix &&a, Matrix &&b );
Matrix&& operator+(const Matrix &a, Matrix &&b );
Matrix&& operator+(Matrix &&a, const Matrix &b);
Matrix operator+(const Matrix &a, const Matrix &b);
精彩评论