i = i++; is undefined. Is i = foo(i++) also undefined?

前端 未结 2 1667
太阳男子
太阳男子 2021-02-13 01:27

For example:

int foo(int i) { return i; }

int main()
{
  int i = 0;

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

  return 0;
}

What wou

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-13 01:59

    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 has the answer:

    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 ]

    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.

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

提交回复
热议问题