这些分配之间是否有顺序点? [英] Is there a sequence point between these assignments?
问题描述
以下代码中的两个赋值之间是否有一个序列点:
Is there a sequence point between the two assignments in the following code:
f(f(x=1,1),x=2);
推荐答案
The relevant quote from the (draft) standard [6.5.2.2, 10] is:
功能指示符的求值顺序, 实际的参数和 实际参数未指定,但是有一个序列 在实际通话之前指向.
The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
因此对于您的表达式,第一个参数(尤其是对f
的调用)可能是
在第二个参数之前求值;例如:
So for your expression, the first argument (in particular the call to f
) could be
evaluated before the second argument; e.g.:
(x = 1, 1), f <sp> call, (x = 2), f <sp> call
或者,它可以在第二个参数之后求值;例如:
Or, it could be evaluated after the second argument; e.g.:
(x = 2), (x = 1, 1), f <sp> call, f <sp> call
[函数调用本身可以(而且很可能会)包含更多的序列点(特别是如果包含return
语句的话).]
[The function call itself can (and most probably will) contain more sequence points (in particular if it contains a return
statement).]
据此,分配之间是否存在一个序列点. 这取决于平台(未指定").
Depending on that, there is a sequence point between the assignments or not. It is up to the platform ("unspecified").
由于在第二种情况下,您要在两个序列点之间两次分配给x
,所以您在这种平台上有未定义的行为.
Since in the 2nd case, you are assigning to x
twice between two sequence points, you have undefined behaviour on such a platform.
这篇关于这些分配之间是否有顺序点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!