复制省略只能在少数特定情况下发生,其中最常见的是临时复制(其他是返回本地,以及抛出/捕获异常)。您的代码没有临时生成,因此不会省略任何副本。
复制构造函数被调用是因为foo
没有移动构造函数(移动构造函数不会为具有显式复制构造函数的类隐式生成),因此 std::move(a)
匹配foo(const foo &rhs)
构造函数(用于构造函数参数)。
在以下情况下可以省略左值的副本(尽管没有办法强制编译器执行省略):
foo fn() {
foo localAutomaticVariable;
return localAutomaticVariable; //Copy to construct return value may be elided
}
int main() {
try {
foo localVariable;
throw localVariable; //The copy to construct the exception may be elided
}
catch(...) {}
}
如果你想在传递函数参数时避免复制,你可以使用一个移动构造函数来窃取给它的对象的资源:
class bar {
public:
bar() {cout<<"ctor"<<endl;};
bar(const bar &rhs) {cout<<"copy ctor"<<endl;}
bar(bar &&rhs) {cout<<"move ctor"<<endl;}
};
void fn(bar a)
{
}
//Prints:
//"ctor"
//"move ctor"
int main()
{
bar b;
f(std::move(b));
}
此外,只要允许复制省略但不发生复制省略,则将使用移动构造函数(如果可用)。