考虑以下:
ComplexObject foo()
{
ComplexObject temp;
//Do things with temp
ComplexObject result(temp, SOME_OTHER_SETTING); //1
//Do things with result. Do not use temp at all
return result; //2
}
ComplexObject foo()
{
ComplexObject temp;
//Do things with temp
ComplexObject result(std::move(temp), SOME_OTHER_SETTING); //1
//Do things with result. Do not use temp at all
return std::move(result); //2
}
假设 ComplexObject 有一个移动构造函数,它比它的复制构造函数效率高得多。
是否允许编译器有效地将第一个代码转换为第二个代码,因为它知道 ComplexObject 不能用于该块的其余部分?