为了账号安全,请及时绑定邮箱和手机立即绑定

array[j + 1] = array[j--]; 这个语句有问题么?

array[j + 1] = array[j--]; 这个语句有问题么?

C++
慕工程0101907 2019-03-15 17:45:13
int j; ... ...array[j + 1] = array[j--];array[j + 1] = array[j--]是否一定等价于array[j + 1] = array[j]; j--;?
查看完整描述

2 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

C++17之前是UB。C++17确定了顺序:赋值操作符的右侧操作数先序于左侧。所以左侧得到的是自减后的值。故而array[j + 1] = array[j--];一般等价于(并发例外)

array[j] = array[j];
--j;
8.18.1[expr.ass] ...In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. The right operand is sequenced before the left operand...


查看完整回答
反对 回复 2019-03-15
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

array[j + 1] = array[j--]; 属于未定义行为。
两个顺序点之间访问和修改了同一个对象。

查看完整回答
反对 回复 2019-03-15
  • 2 回答
  • 0 关注
  • 991 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信