Is there a sequence point between these assignments?

前端 未结 6 1724

Is there a sequence point between the two assignments in the following code:

f(f(x=1,1),x=2);
6条回答
  •  后悔当初
    2021-02-13 10:03

    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.

    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  call, (x = 2), f  call
    

    Or, it could be evaluated after the second argument; e.g.:

    (x = 2), (x = 1, 1), f  call, f  call
    

    [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").

    Since in the 2nd case, you are assigning to x twice between two sequence points, you have undefined behaviour on such a platform.

提交回复
热议问题