c – 为什么复制elision不能使用std :: move?

c – 为什么复制elision不能使用std :: move?,第1张

概述我使用下面的代码来测试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:: 我使用下面的代码来测试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));}

此外,每当允许复制省略但未发生复制省略时,将使用移动构造函数(如果可用).

总结

以上是内存溢出为你收集整理的c – 为什么复制elision不能使用std :: move?全部内容,希望文章能够帮你解决c – 为什么复制elision不能使用std :: move?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1258634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存