Is default constructor elision / assignment elision possible in principle?

前端 未结 4 1343
小蘑菇
小蘑菇 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:26

    Jan Hudec's answer has a very good point about the as-if rule (+1 for him).

    Hence, the assignment elision is allowed -- as he said -- provided that there's no observable effect. The fact that your assignment operator outputs "ASSIGN" is enough to prevent the optimization.

    Notice that the situation is different for copy/move constructors because the standard allows elision for copy/move constructor even if they have observable side effects (see 12.8/31).

提交回复
热议问题