我使用下面的代码来测试copy elision:
class foo
{
public:
foo() {cout<<"ctor"<<endl;};
foo(const foo &rhs) {cout<<"copy ctor"<<endl;}
};
int g(foo a)
{
return 0;
}
int main()
{
foo a;
g(std::move(a));
return 0;
}
我预计只会调用默认构造函数,因为g()的参数是一个rvalue,并且将省略复制.但结果显示默认构造函数和复制构造函数都被调用.为什么?
如果我将函数调用更改为g(foo()),则复制将被删除. foo()和std :: move(a)的返回类型有什么区别?如何在左值上编译编译器?
解决方法
复制省略只能在少数特定情况下发生,其中最常见的是复制临时(其他人正在返回本地,以及抛出/捕获异常).您的代码没有临时生成,因此不会删除任何副本.
正在调用复制构造函数,因为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));
}
此外,每当允许复制省略但未发生复制省略时,将使用移动构造函数(如果可用).