是(++ i)++未定义的行为吗? [英] Is (++i)++ undefined behavior?

查看:158
本文介绍了是(++ i)++未定义的行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(++ 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中是未定义的 - 在C ++ 0x中定义。

My gut feeling says this is undefined in C++03 and well-defined in C++0x.

是的,你是对的。在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)++未定义的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆