The order of evaluation of f (g (), h ()) in C ++

This is a kind of continuation. Can a C ++ implementation theoretically parallelize the evaluation of two function arguments?

Suppose I have the following C ++ 11 program:

#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()); } 

Of course, the following outputs are observed:

 1 3 2 4 5 2 4 1 3 5 

How about 1 2 3 4 5?

(As far as I can tell, the only limitations are that 1 is sequenced to 3, 2 is sequenced to 4, and both 3 and 4 are sequenced to 5.)

+4
source share
1 answer

C ++ 03 normally said that invocations do not alternate. C ++ 11 normatively states that

Each estimate in the calling function (including other function calls) that is otherwise not separately secreted before or after the body of the called function is executed is indefinitely ordered with respect to the execution of the called function.

Both are enough to ban it.

+8
source

Source: https://habr.com/ru/post/1446809/


All Articles