Is default constructor elision / assignment elision possible in principle?

前端 未结 4 1360
小蘑菇
小蘑菇 2020-12-11 04:53

. or even allowed by the C++11 standard?

And if so, is there any compiler that actually does it?

Here is an example of what I mean:

template&         


        
4条回答
  •  醉酒成梦
    2020-12-11 05:20

    There is an important drawback to what you propose: what would happen if the constructor threw? The behaviour for that case is well defined in the standard (all data members that had already been constructed are destructed in reverse order), but how would that translate into the case where we are "constructing" the object into an already existing one?

    Your example is easy because the constructor cannot throw when T = double, but this is not so in the general case. You might end up with a half destructed object and undefined behaviour would ensue, even if your constructor and assignment operator were well behaved.

提交回复
热议问题