Post-increment and Pre-increment concept?(后增量和前增量概念?)
问题描述
我不明白后缀和前缀递增或递减的概念.谁能给个更好的解释?
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.
这篇关于后增量和前增量概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:后增量和前增量概念?


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01