i = i ++;未定义。是i = foo(i ++)也未定义? [英] i = i++; is undefined. Is i = foo(i++) also undefined?

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

问题描述

例如:

int foo(int i) { return i; }

int main()
{
  int i = 0;

  i = i++;      // Undefined
  i = foo(i++); // ?

  return 0;
}

当前的ISO C ++标准对这种情况指定什么?

What would the current ISO C++ standard specify for this case?

编辑:

这里是我困惑的地方:


除非另有说明,否则单个运算符的操作数和单个表达式的子表达式的计算结果不受影响。

Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced.

对标量
对象的影响相对于对同一标量对象的另一个副作用或使用同一个标量对象的值的值计算
是不定的,并且它们不是潜在并发的(1.10),行为是
未定义。

If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, and they are not potentially concurrent (1.10), the behavior is undefined.

在所有情况下,赋值在右值和左值操作数的值
计算之后,在赋值表达式

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

在调用函数(包括其他函数调用)中的每个评估在执行被调用函数的正文之前或之后没有另外具体地
顺序,不确定地排序
相对于被调用函数的执行。

Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.

所以看来你可以在左侧( i )和右侧的副作用( i 从<$ c

So it seems you could have a value computation on the left side of the assignment (just i), and a side effect on the right side (the modification of i from i++) which aren't sequenced with respect to each other.

EDIT2

对于发现自己在这里的任何人,都有一个非常好的解释,我发现此处

For anyone who finds themselves here, there is a really great explanation about sequencing that I found here.

推荐答案

您的报价中的最后一句话是在被调用函数的主体的执行之前或之后没有特别地排序,因此问题是增量和赋值是否否则在函数体之前或之后特定地排序。

The last sentence in your quote says "that is not otherwise specifically sequenced before or after the execution of the body of the called function" so the question is whether the increment and the assignment are "otherwise specifically sequenced before or after" the function body.

1.9 [intro.execution] p15有答案:

1.9 [intro.execution] p15 has the answer:


函数(无论函数是否为内联函数),与任何参数表达式或与指定调用函数的后缀表达式相关联的每个值计算和副作用
是在执行每个表达式之前排序的
语句在被调用函数的正文中。 [注意:价值
与不同参数表达式相关的计算和副作用不受影响。 - 结束注释]

因此 i < c>发生在函数体之前,并且 i 的赋值发生在函数返回之后,因此它是完全明确的。

So the increment of i happens before the function body, and the assignment to i happens after the function returns, so it is perfectly well-defined.

在前C ++ 11术语中,函数调用在增量和赋值之间引入一个序列点。

In pre-C++11 terminology, the function call introduces a sequence point between the increment and the assignment.

这篇关于i = i ++;未定义。是i = foo(i ++)也未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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