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

前端 未结 2 1664
太阳男子
太阳男子 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 02:01

    i = foo(i++); is fine, because i++ is executed before foo() is called. A copy of i is made, i is then incremented, then the copy is passed to foo(). It is the same as doing this explicitly:

    int tmp = i++;
    i = foo(tmp);
    

提交回复
热议问题