As-if rule
wikipedia As-if rule
The standard for the C++ programming language allows compilers for this language to apply any optimizing[1] transformation to a program during compilation, provided that such optimizations make no change in the "observable behavior" of the program, as specified in the standard; this mostly means that any actions the program performs on its environment occur in the specified order. This rule is commonly referred to as the as-if rule.[2]
NOTE: 这对于理解很多compiler optimization是非常重要的
cppreference The as-if rule
Explanation
NOTE: 此处的解释是更加详细的。
Exception
本段总结打破"As-if rule"的一些optimization,重要参考了如下文章:
1、wikipedia As-if rule
2、cppreference Copy elision # Notes
3、stackoverflow What are copy elision and return value optimization? # A
wikipedia As-if rule
The rule has three main exceptions.
The first is that programs exhibiting(展示) undefined behavior are exempt(豁免的); since the observable behavior is not well-defined anyway, any transformation is valid.
NOTE: 首先,程序表现出不明确的行为是豁免的; 由于可观察行为没有明确定义,*任何*转换都是有效的。
The other two exceptions concern the copying of objects, and are called copy elision and the return value optimization.
NOTE: 通过下面的内容可知,上述总结是不全面的,或者说不是最新的
cppreference Copy elision # Notes
Copy elision is the only allowed form of optimization (until C++14)one of the two allowed forms of optimization, alongside allocation elision and extension, (since C++14) that can change the observable side-effects. Because some compilers do not perform copy elision in every situation where it is allowed (e.g., in debug mode), programs that rely on the side-effects of copy/move constructors and destructors are not portable.
NOTE: 上面这段话,初读是难以理解的,下面是注解版本:
Optimization that can change the observable side-effects:
一、until C++14
1、Copy elision
二、since C++14
1、Copy elision
2、Allocation elision and extension
"Optimization that can change the observable side-effects"意味中打破As-if-rule,因此,这段话总结了compiler打破As-if rule的optimization。