我=我++;未定义。 i = foo(i++) 也未定义吗?
i = i++; is undefined. Is i = foo(i++) also undefined?
例如:
int foo(int i) { return i; }
int main()
{
int i = 0;
i = i++; // Undefined
i = foo(i++); // ?
return 0;
}
当前的 ISO C++ 标准对这种情况有何规定?
编辑:
这是我感到困惑的地方:
Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced.
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
的修改来自 i++
) 没有相对于彼此排序。
EDIT2:
对于任何发现自己在这里的人,我发现了关于排序的非常好的解释 here。
i = foo(i++);
没问题,因为 i++
在调用 foo()
之前执行。制作了 i
的副本,然后递增 i
,然后将副本传递给 foo()
。这与显式执行此操作相同:
int tmp = i++;
i = foo(tmp);
你引用的最后一句话说 "that is not otherwise specifically sequenced before or after the execution of the body of the called function" 所以问题是递增和赋值是否是 "otherwise specifically sequenced before or after" 函数体。
1.9 [intro.execution]p15有答案:
When calling a function (whether or not the function is inline), every value computation and side effect
associated with any argument expression, or with the postfix expression designating the called function, is
sequenced before execution of every expression or statement in the body of the called function. [ Note: Value
computations and side effects associated with different argument expressions are unsequenced. — end note ]
所以i
的自增发生在函数体之前,而对i
的赋值发生在函数returns之后,所以它是完美定义的。
在 C++11 之前的术语中,函数调用在增量和赋值之间引入了一个序列点。
例如:
int foo(int i) { return i; }
int main()
{
int i = 0;
i = i++; // Undefined
i = foo(i++); // ?
return 0;
}
当前的 ISO C++ 标准对这种情况有何规定?
编辑:
这是我感到困惑的地方:
Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced.
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
的修改来自 i++
) 没有相对于彼此排序。
EDIT2:
对于任何发现自己在这里的人,我发现了关于排序的非常好的解释 here。
i = foo(i++);
没问题,因为 i++
在调用 foo()
之前执行。制作了 i
的副本,然后递增 i
,然后将副本传递给 foo()
。这与显式执行此操作相同:
int tmp = i++;
i = foo(tmp);
你引用的最后一句话说 "that is not otherwise specifically sequenced before or after the execution of the body of the called function" 所以问题是递增和赋值是否是 "otherwise specifically sequenced before or after" 函数体。
1.9 [intro.execution]p15有答案:
When calling a function (whether or not the function is inline), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function. [ Note: Value computations and side effects associated with different argument expressions are unsequenced. — end note ]
所以i
的自增发生在函数体之前,而对i
的赋值发生在函数returns之后,所以它是完美定义的。
在 C++11 之前的术语中,函数调用在增量和赋值之间引入了一个序列点。