问题描述
是(++ i)++
未定义的行为吗?是否可能的前缀增量的副作用发生后检索递增对象后缀增量操作?这对我来说看起来很奇怪。
Is (++i)++
undefined behavior? Is it possible that the side effect of prefix increment happens after retrieving the incremented object for postfix increment to operate on? That would seem strange to me.
我的内心感觉说这在C ++ 03中是未定义的,在C ++ 11中定义得很好。
My gut feeling says this is undefined in C++03 and well-defined in C++11. Am I right?
推荐答案
是的,你是对的。在C ++ 03中的行为是未定义的,因为您试图在两个序列点之间多次修改 i
。
Yes you are right. The behaviour is undefined in C++03 because you are trying to modify i
more than once between two sequence points.
由于(++ i)++
等价于(i + = 1)++
。 + =
运算符的副作用相对于 ++
(后递增)排序,因此行为很好定义。
The behaviour is well defined in C++0x because (++i)++
is equivalent to (i += 1)++
. The side effects of the +=
operator are sequenced relative to ++
(post increment) and so the behaviour is well defined.
这篇关于是(++ i)++未定义的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!