摘自维基百科:
In C[4] and C++,[5] sequence points occur in the following places. (In C++, overloaded operators act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.)
- Between evaluation of the left and right operands of the && (logical AND), || (logical OR), and comma operators. For example, in the expression
*p++ != 0 && *q++ != 0
, all side effects of the sub-expression*p++ != 0
are completed before any attempt to accessq
. - Between the evaluation of the first operand of the ternary "question-mark" operator and the second or third operand. For example, in the expression
a = (*p++) ? (*p++) : 0
there is a sequence point after the first*p++
, meaning it has already been incremented by the time the second instance is executed. - At the end of a full expression. This category includes expression statements (such as the assignment
a=b;
), return statements, the controlling expressions ofif
,switch
,while
, ordo
-while
statements, and all three expressions in afor
statement. - Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered. In the expression
f(i++) + g(j++) + h(k++)
,f
is called with a parameter of the original value ofi
, buti
is incremented before entering the body off
. Similarly,j
andk
are updated before enteringg
andh
respectively. However, it is not specified in which orderf()
,g()
,h()
are executed, nor in which orderi
,j
,k
are incremented. Variablesj
andk
in the body off
may or may not have been already incremented. Note that a function callf(a,b,c)
is not a use of the comma operator and the order of evaluation fora
,b
, andc
is unspecified. - At a function return, after the return value is copied into the calling context. (This sequence point is only specified in the C++ standard; it is present only implicitly in C.[6])
- At the end of an initializer; for example, after the evaluation of
5
in the declarationint a = 5;
. - Between each declarator in each declarator sequence; for example, between the two evaluations of
a++
inint x = a++, y = a++
[7].
翻译:
在c和c++中,sequence points发生在下列位置。(在c++中重载操作符的行为就像函数,这些操作符的sequence points规则同函数调用)
- 逻辑与(&&),逻辑或(||)和逗号(,)的左边和右边的赋值之间。
- 在(?:)表达式中,?左边的赋值与右边的任一表达式之间。
- 在完整表达式(分号,return表达式,if, switchm while, do-while, for的3个表达式中的每一个)的后面---不能是子表达式。
- 在一个函数调用时,进入函数体之前。函数的参数调用顺序并未指定(应该指的是一个表达式中的多个函数调用各自的参数)。这个sequence point的意思是,在每个函数进入之前,它们参数的副作用都已经处理完毕。如: f(i++)+g(j++)+h(k++), 在f()进入之后,i一定已经增加了,但是此时在f()内部,j 和 k可能还没有增加,同时,在整个表达式中f(), g(), h()的调用顺序是不确定的。i++, j++, k++的调用顺序也是不确定的。。
- 在一个函数return的时候,回到调用程序中之后(只在c++中有效,在c中是隐含(啥叫隐含)的)。
- 初始化语句之后,如 int a = 4; 的后边。
- 在声明语句的每一个声明之间。如: int a = 4, b = a++, c = a++;这是正确的。