gpt4 book ai didi

c++ - C++ 中 f(g(), h()) 的求值顺序

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:19 25 4
gpt4 key购买 nike

这是 Could a C++ implementation, in theory, parallelise the evaluation of two function arguments? 的后续行动

假设我有以下 C++11 程序:

#include <cstdio>
using std::printf;
int g() { printf("1\n"); printf("3\n"); return 2; }
int h() { printf("2\n"); printf("4\n"); return 3; }
void f(int a, int b) { printf("%i\n", a+b); }
int main() { f(g(), h()); }

当然可以观察到以下输出:

1 3 2 4 5
2 4 1 3 5

1 2 3 4 5 呢?

(据我所知,唯一的限制是 1 排在 3 之前,2 排在 4 之前,3 和 4 都排在 5 之前。)

最佳答案

C++03 确实明确表示函数调用不会交错。 C++11 规范地说

Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.

两者都足以禁止这样做。

关于c++ - C++ 中 f(g(), h()) 的求值顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13443833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com