What optimization does move semantics provide if we already have RVO?

前端 未结 8 1871
北海茫月
北海茫月 2020-12-04 17:50

As far as I understand one of the purposes of adding move semantics is to optimize code by calling special constructor for copying \"temporary\" objects. For example, in th

8条回答
  •  难免孤独
    2020-12-04 18:39

    This line calls the first constructor.

    stuff a(5),b(7);
    

    Plus operator is called using explicit common lvalue references.

    stuff c = a + b;
    

    Inside operator overload method, you have no copy constructor called. Again, the first constructor is called only.

    stuff g(lhs.x+rhs.x);
    

    assigment is made with RVO, so no copy is need. NO copy from returned object to 'c' is need.

    stuff c = a+b;
    

    Due no std::cout reference, compiler take care about your c value is never used. Then, whole program is stripped out, resulting in a empty program.

提交回复
热议问题