加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

未定义的行为,或者:Swift是否有序列点?

发布时间:2020-12-14 05:48:12 所属栏目:百科 来源:网络整理
导读:在C/C++中,第二个声明在 int i = 0;int j = i++ + i++ + ++i; 调用两者 未指定的行为,因为操作数的评估顺序 是未指定的,并且 未定义的行为,因为对同一对象的副作用我相对于彼此没有排序. 例如,参见 Why are these constructs (using ++) undefined behavior?
在C/C++中,第二个声明在
int i = 0;
int j = i++ + i++ + ++i;

调用两者

>未指定的行为,因为操作数的评估顺序
是未指定的,并且
>未定义的行为,因为对同一对象的副作用我相对于彼此没有排序.

例如,参见

> Why are these constructs (using ++) undefined behavior?
> Undefined behavior and sequence points

现在,鉴于Swift被设计为一种安全的语言,那么它是什么
相应的情况在这?结果是

var i = 0
let j = i++ + i++ + ++i

明确界定?可以从语言参考中得出结论
swift书j == 4?

Apple开发人员和Swift设计师Chris回答了这个问题
Lattner在Apple开发者论坛 https://forums.developer.apple.com/thread/20001#63783中:

Yes,the result of that expression will always be 4. Swift evaluates
expressions left to right,it isn’t undefined or implementation
defined behavior like C.

克里斯还补充道:

That said,if you write code like that,someone trying to maintain it
will probably not be very happy with you

同意!这是一个证明问题的极端例子.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读