本文介绍了后增量和前增量概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白后缀和前缀递增或递减的概念.谁能给个更好的解释?

I don't understand the concept of postfix and prefix increment or decrement. Can anyone give a better explanation?

推荐答案

到目前为止,所有四个答案都不正确,因为它们断言了特定的事件顺序.

All four answers so far are incorrect, in that they assert a specific order of events.

相信都市传奇"已经让许多新手(和专业人士)误入歧途,也就是说,关于表达式中的未定义行为的问题层出不穷.

Believing that "urban legend" has led many a novice (and professional) astray, to wit, the endless stream of questions about Undefined Behavior in expressions.

所以.

对于内置的 C++ 前缀运算符,

For the built-in C++ prefix operator,

++x

递增 x 并产生(作为表达式的结果)x 作为左值,而

increments x and produces (as the expression's result) x as an lvalue, while

x++

递增 x 并产生(作为表达式的结果)x 的原始值.

increments x and produces (as the expression's result) the original value of x.

特别是,对于x++x 的原始值的递增和产生并不意味着没有时间顺序.编译器可以自由地发出产生 x 原始值的机器代码,例如它可能存在于某个寄存器中,这会将增量延迟到表达式结束(下一个序列点).

In particular, for x++ there is no no time ordering implied for the increment and production of original value of x. The compiler is free to emit machine code that produces the original value of x, e.g. it might be present in some register, and that delays the increment until the end of the expression (next sequence point).

错误地认为增量必须是第一位的人,而且他们有很多人,通常会得出结论,某些表达式必须具有明确定义的效果,而实际上它们具有未定义的行为.

Folks who incorrectly believe the increment must come first, and they are many, often conclude from that certain expressions must have well defined effect, when they actually have Undefined Behavior.

这篇关于后增量和前增量概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 20:08