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

前端 未结 8 1877
北海茫月
北海茫月 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:55

    There are many places some of which are mentioned in other answers.

    One big one is that when resizing a std::vector it will move move-aware objects from the old memory location to the new one rather than copy and destroy the original.

    Additionally rvalue references allow the concept of movable types, this is a semantic difference and not just an optimization. unique_ptr wasn't possible in C++03 which is why we had the abomination of auto_ptr.

提交回复
热议问题