Could a C++ implementation, in theory, parallelise the evaluation of two function arguments?

后端 未结 4 1858
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 14:14

Given the following function call:

f(g(), h())

since the order of evaluation of function arguments is unspecified (still the case in C++11

4条回答
  •  余生分开走
    2020-12-23 15:10

    Not unless the compiler knew exactly what g(), h(), and anything they call does.

    The two expressions are function calls, which may have unknown side effects. Therefore, parallelizing them could cause a data-race on those side effects. Since the C++ standard does not allow argument evaluation to cause a data-race on any side effects of the expressions, the compiler can only parallelize them if it knows that no such data race is possible.

    That means walking though each function and look at exactly what they do and/or call, then tracking through those functions, etc. In the general case, it's not feasible.

提交回复
热议问题