Will member subobjects of local variables be moved too if returned from a function?

前端 未结 1 868
眼角桃花
眼角桃花 2020-12-30 12:44

The C++11 standard states that, if the conditions for copy elision are met (§12.8/31), the implementation shall treat a returned local lvalue varia

相关标签:
1条回答
  • 2020-12-30 13:08

    When returning a subobject you can't elide its construction. Think of it this way: move and copy elision essentially amount to constructing the object in the place it would eventually be moved or copied to. This works for complete objects because there will be the appropriate space be set aside. It doesn't work with subobjects because you would construct the enclosing object. Even if this has the same size as the subobject, i.e. there is enough space, the enclosing object gets destroyed and may do funny things to the subobjects.

    Effectively, this means that construction of the subject cannot be elided.

    0 讨论(0)
提交回复
热议问题