C++ 请教关于移动构造函数的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wisefree
V2EX    C++

C++ 请教关于移动构造函数的问题

  •  
  •   wisefree 2021-05-02 13:37:55 +08:00 2438 次点击
    这是一个创建于 1691 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Complex c{ Complex{1, 2} }; 

    为什么这条语句不会进入移动构造函数呢?

    1. 用 g++,关闭了优化 g++ -O0 main.cpp

    2. 用 visual studio 2017,也关闭了优化

    #include<iostream> using std::cout; using std::endl; class Cmplex { public: Complex(double real, double image) : _real(real), _image(image) {} Complex(const Complex& right); Complex(Complex&& right); Complex& operator=(const Complex& right); Complex& operator=(Complex&& right); virtual ~Complex(); private: double _real {}; double _image {}; }; Complex::Complex(const Complex& right) { cout << "&" << endl; _real = right._real; _image = right._image; } Complex::Complex(Complex&& right) { cout << "&&" << endl; _real = right._real; _image = right._image; } Complex& Complex::operator= (const Complex& right) { cout << "= &" << endl; if (this != &right) { _real = right._real; _image = right._image; } return *this; } Complex& Complex::operator= (Complex&& right) { cout << "= &&" << endl; if (this != &right) { _real = right._real; _image = right._image; } return *this; } Complex::~Complex() { cout << "des" << endl; } int main() { Complex c{ Complex{1, 2} }; //Complex c {std::move(Complex{1, 2})}; cout << &c << endl; return 0; } 
    16 条回复    2021-06-14 20:28:39 +08:00
    edimetia3d
        1
    edimetia3d  
       2021-05-02 13:50:21 +08:00   2
    没必要关注这种 case, 八成是编译器把初始化优化了.
    leimao
        2
    leimao  
       2021-05-02 13:54:41 +08:00   1
    当 argument 是 lvalue 的时候,invoke copy constructor; 当 argument 是 rvalue 的时候,invoke move constructor;。
    https://leimao.github.io/blog/CPP-lvalue-rvalue-Reference/
    leimao
        3
    leimao  
       2021-05-02 14:00:04 +08:00
    确切的说应该叫 lvalue reference 和 rvalue reference 。
    wisefree
        4
    wisefree  
    OP
       2021-05-02 14:03:30 +08:00
    @edimetia3d 我也感觉是被优化了,所以关闭了优化,但是没有应有的效果。有其他的思路么?
    wisefree
        5
    wisefree  
    OP
       2021-05-02 14:03:55 +08:00
    @leimao 问题是为什么没有 invoke 成功?
    leimao
        6
    leimao  
       2021-05-02 14:14:02 +08:00
    @wisefree 我想你问的应该是为什么 Complex c{ Complex{1, 2} };没有 invoke copy constructor ?
    leimao
        7
    leimao  
       2021-05-02 14:37:42 +08:00
    @leimao 我跑了一下程序看了下结果想了想,一楼应该是对的。估计编译器把
    ```
    Complex c{ Complex{1, 2} };
    ```
    优化成了
    ```
    Complex c{ 1, 2 };
    ```
    所以你啥也没 print 出来。
    leimao
        8
    leimao  
       2021-05-02 14:46:15 +08:00   1
    @wisefree 个人想法:
    碰到你这种奇怪的没必要的 construction 的方式,`Complex c{ Complex{1, 2} };`,编译器必须把你的语法“优化”(改)成以下两种之一。
    1. `Complex c{1, 2};`
    2. `Complex c_temp{1, 2}; Complex c{c_temp}; `
    不然你这个程序没法运行。
    至于“优化”成 1 还是 2, 可能得看写编译器的人怎么写了。当然估计 100%的人会写 1, 因为 2 这个临时变量是完全没有必要的,浪费资源和 performance 。
    lcdtyph
        9
    lcdtyph  
       2021-05-02 15:08:49 +08:00   4
    你需要关闭 constructor elision
    g++ -fno-elide-constructors --std=c++11 ....

    在 c++17 之后你例子里的代码会被 guaranteed copy elision 保证干掉,所以就算加了这个参数也没用,因为已经不是优化而是标准之一了。在此之前都是编译器自己的 constructor elision 在作用。
    lcdtyph
        10
    lcdtyph  
       2021-05-02 15:11:26 +08:00   1
    msvc 我不会用,不知道对应的选项是什么- -
    qieqie
        11
    qieqie  
       2021-05-02 15:42:08 +08:00   1
    copy elision
    wisefree
        12
    wisefree  
    OP
       2021-05-02 21:48:09 +08:00
    @leimao 感谢
    wisefree
        13
    wisefree  
    OP
       2021-05-02 21:49:16 +08:00
    @lcdtyph 用了这个参数,确实可以,厉害了!
    wisefree
        14
    wisefree  
    OP
       2021-05-02 21:49:31 +08:00
    @qieqie 谢啦
    jones2000
        15
    jones2000  
       2021-05-03 19:01:08 +08:00
    你这个要完成什么功能? 用最简单的语法完成你想要功能不就可以了. 怎么高深的语法, 有几个人会用呀?
    shrikextl
        16
    shrikextl  
       2021-06-14 20:28:39 +08:00
    纠结这个干啥,移动构造又不是给你做这种构造写法的,想测试移动构造函数的方法多的很
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5270 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 06:01 PVG 14:01 LAX 22:01 JFK 01:01
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86