首先,我为过于冗长的问题道歉.我想不出任何其他方法来准确地总结我的问题......现在谈到实际的问题:
我目前正在尝试使用C++ 0x rvalue引用...以下代码会产生不需要的行为:
#include <iostream>
#include <utility>
struct Vector4
{
float x, y, z, w;
inline Vector4 operator + (const Vector4& other) const
{
Vector4 r;
std::cout << "constructing new temporary to store result"
<< std::endl;
r.x = x + other.x;
r.y = y + other.y;
r.z = z + other.z;
r.w = w + other.w;
return r;
}
Vector4&& operator + (Vector4&& other) const
{
std::cout << "reusing temporary 2nd operand to store result"
<< std::endl;
other.x += …Run Code Online (Sandbox Code Playgroud) mingw operator-overloading rvalue-reference move-semantics c++11