是 (++i)++未定义的行为?
(++i)++
是未定义的行为吗?前缀增量的副作用是否可能在检索增量对象以进行后缀增量操作后发生?这对我来说似乎很奇怪。
我的直觉告诉我,这在 C++03 中未定义,而在 C++11 中定义良好。我说得对吗?
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.
My gut feeling says this is undefined in C++03 and well-defined in C++11. Am I right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的你是对的。该行为在 C++03 中未定义,因为您尝试在两个序列点之间多次修改
i
。该行为在 C++11 中得到了明确定义,因为
(++i)++
等效于(i += 1)++
。+=
运算符的副作用是相对于++
(后增量)排序的,因此行为得到了明确的定义。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.The behaviour is well defined in C++11 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
在两个序列点之间被修改多次。This is an undefined behaviour since
i
is being modified more than once between two sequence points.