在 C++ 中使用 <execution> 头文件的问题

问题描述

我在 Windows 10 上有 gcc 10.2.0。所以,在这个最新版本的 gcc 中实现了。
问题是当我从以下链接复制示例代码时:https://docs.w3cub.com/cpp/algorithm/reduce,它说:std::execution 尚未声明。有什么想法吗?

#include <iostream>
#include <chrono>
#include <vector>
#include <numeric>
#include <execution>
 
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::execution::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";
    }
}

解决方法

为了向后兼容,许多编译器默认使用旧的 C++ 模式。在设置中启用 C++17 以使用更新的功能。