c++ - 如何使用 g++6 使用 std::reduce 和 #include <execution_policy> 编译代码?

标签 c++ stl c++17

有人能告诉我如何为 std::reduce 编译示例代码吗? , 在 cppreference 上?

#include <iostream>
#include <chrono>
#include <vector>
#include <numeric>
#include <execution_policy>

int main()
{
    std::vector<double> v(10'000'007, 0.5);

    {
        auto t1 = std::chrono::high_resolution_clock::now();
        double result = std::accumulate(v.begin(), v.end(), 0.0);
        auto t2 = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double, std::milli> ms = t2 - t1;
        std::cout << std::fixed << "std::accumulate result " << result
                  << " took " << ms.count() << " ms\n";
    }

    {
        auto t1 = std::chrono::high_resolution_clock::now();
        double result = std::reduce(std::par, v.begin(), v.end());
        auto t2 = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double, std::milli> ms = t2 - t1;
        std::cout << "std::reduce result "
                  << result << " took " << ms.count() << " ms\n";
    }
}

我在 Mint18 下工作,安装了新的 g++6.2。在编译期间我只使用 -std=c++17旗帜。想用execution_policy怎么办? (#include <execution_policy>)?目前我的编译器告诉我 reduce不是命名空间 std 的成员并且没有 execution_policy 这样的文件.

最佳答案

libstdc++(默认与 g++ 一起使用)和 libc++(默认与 clang 一起使用)都没有实现 execution_policy 支持。

this page 上跟踪了在 libstdc++ 中实现新功能的进展情况.跟踪 libc++ 进度 here .如您所见,截至撰写本文时,P0024R2“并行 TS 应标准化”尚未由任何一个库实现。

关于c++ - 如何使用 g++6 使用 std::reduce 和 #include <execution_policy> 编译代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41081317/

相关文章:

c++ - 查找素数和时出现内存错误

c++ - 单元测试线程: how to confirm that a thread was blocked

c++ - 通过网络构建和发送二进制数据

c++ - 为什么 std::allocator::deallocate 需要大小?

c++ - [[maybe_unused]] 在枚举器上

c++ - 在固定的、无序的、拥有的数组中安全、惯用的销毁和压缩

c++ - 在 C++ 中将所有和任何命令行输入转换为字符串

c++ - 如何为 std::vector 分配内存,然后为某些元素调用构造函数?

c++ - 使用 std::vector 作为 std::map 的键在未找到和使用 reserve() 时不返回 end()

C++17 模板推导指南不用于空参数集?