c++ - 理论上,C++ 实现能否并行计算两个函数参数?

标签 c++ language-lawyer

给定以下函数调用:

f(g(), h())

由于未指定函数参数的评估顺序(据我所知,在 C++11 中仍然如此),理论上是否可以执行 g() h() 并行?

这种并行化只能在 gh 已知相当琐碎(在最明显的情况下,仅访问其主体的本地数据)的情况下启动,以便不是要引入并发问题,但除了这个限制之外,我看不到任何禁止它的东西。

那么,标准允许吗?即使只是按照 as-if 规则?

(在 this answer 中,Mankarse 断言并非如此;但是,他没有引用标准,并且我对 [expr.call] 的通读没有发现任何明显的措辞.)

最佳答案

需求来自[intro.execution]/15:

... When calling a function ... 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 [Footnote: In other words, function executions do not interleave with each other.].

所以 g() 主体的任何执行都必须与 h() 的求值顺序不确定(即不重叠)(因为 >h() 是调用函数中的表达式)。

这里的关键点是 g()h() 都是函数调用。

(当然,as-if 规则意味着不能完全排除这种可能性,但它绝不应该以可能影响程序可观察行为的方式发生。这样的实现最多只会改变代码的性能特征。)

关于c++ - 理论上,C++ 实现能否并行计算两个函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13443532/

相关文章:

c++ -++x %= 10 在 C++ 中是否明确定义?

c - NULL 参数对 strstr 的行为是什么?

c - 返回指向局部变量的指针是UB吗?

c++ - 大括号初始化时 GCC 出现编译错误

c++ - 更改此代码后,为什么会给我 “segmentation fault”错误?

c++ - 如何提升cocos2dx中位图纹理渲染的性能

c++ - 如何修复无法从 `int to ` PUCHAR 转换参数 1?

C++ Boost::Asio 我得到了 remote.Endpoint().address,现在如何解析主机名?

c++ - 警告在 WDK 构建 ("LNK4217: locally defined symbol _ imported in function _"中链接 boost 库)

c++ - 复制 std::string 时的 MSVC 12 std::initializer_list 错误