Move Constructor vs Copy Elision. Which one gets called?

后端 未结 2 1868
灰色年华
灰色年华 2020-12-10 08:05

I have two pieces of code here to show you. They are two classes and each one provides a Move Constructor and a function which returns a temporary.

  • In the first
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 08:24

    The copy elision is an optimization that, nowadays, every modern compiler provides.

    When returning huge class objects in C++, this technique applies... but not in every case!

    In the first example, the compiler performs the Move Constructor because we have more than one return statement in the function.

提交回复
热议问题