且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

后缀增量是否不对返回值执行增量?

更新时间:2023-02-20 22:41:09

i = i++;

未定义。如果单个表达式为变量赋予两个不同的值,C ++规范说任何事情都可能发生 - 它可以取其旧值,两个新值之一,或几乎任何东西。这样做的原因是它允许编译器对简单表达式进行更积极的优化。编译器可以重新安排赋值和 ++ 的执行顺序,例如,如果它认为它更有效率。

is undefined. If a single expression assigns two different values to a variable, the C++ spec says that anything can happen - it could take on its old value, one of the two new values, or pretty much anything at all. The reason for this is that it allows the compiler to make much more aggressive optimizations of simple expressions. The compiler could rearrange the order in which the assignment and ++ are executed, for example, if it thought it were more efficient.