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

问题描述

我不明白后缀和前缀增加或减少的概念。

解决方案

迄今为止的所有四个答案都不正确断言一个特定的事件顺序。



相信城市传说已经导致许多新手(和专业)误入歧途,关于未定义行为的无尽的问题



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

  ++ x 

增加 x 并生成作为表达式结果 x 作为左值,而

  x ++ 

x 并生成 x 的原始值作为表达式结果。



x ++ 没有对 x 无时间排序 c>。编译器可以自由地发出生成 x 的原始值的机器码,例如它可能存在于某个寄存器中,并且延迟增量直到表达式结束(下一个序列点)。



那些不正确地相信增量必须先到的人,他们是很多,经常得出结论,某些表达式必须有明确的效果,当他们实际上有未定义的行为。


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.

So.

For the built-in C++ prefix operator,

++x

increments x and produces as expression result x as an lvalue, while

x++

increments x and produces as expression result the original value of 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