c++ - 评估这个的顺序是什么?为什么? C++

标签 c++ function arguments expression-evaluation

int foo(int a, int& b, int c) {
    int temp = a;
    a = b;
    b = c;
    c = temp;
    return a - b;
}

int main() {


**foo(foo(a, b, c), b, foo(a, b, foo(a, b, c)));**


return 0;
}

首先评估哪个 foo 函数调用,为什么? 我发布的代码已简化,因此无需跟踪它。 谢谢

最佳答案

假设 ** 是拼写错误而不是语法错误,并使用以下命名:

 (A)      (B)          (C)       (D) 
foo ( foo(a, b, c), b, foo(a, b, foo(a, b, c)))

以下是正确的:

  • (D) 在 (C) 之前求值,因为调用 (C) 需要参数值。
  • (A) 在 (B) 和 (C) 之后求值(因此 (D) )

不能说更多,因为 C++ 标准让编译器对参数评估进行排序:

5.2.2/4: When a function is called, each parameter shall be initialized with its corresponding argument. [Note: Such initializations are indeterminately sequenced with respect to each other — end note ]

关于c++ - 评估这个的顺序是什么?为什么? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40696973/

相关文章:

c++ - 使用 CascadeClasifier 与 FaceRecognizer (FisherFace) 的 OpenCV 性别分类

Javascript Navigator.geolocation

javascript - HackReactor,编码窗口现在可以使用名为 "Admissions"的方法访问名为 "showApp"的对象。不带任何参数调用此方法?

c++ - C++中双变量的逼近

c++ - 二进制代码错误帮助

c++ - cin.get() 导致 "no instance of overloaded function"错误

c++ - 这两个 C++ 函数的区别

mysql - 创建函数时遇到问题。有人可以帮助我吗?

c# - 为什么未使用的事件参数需要赋值?

nginx - 让 nginx 检查 url 中的参数