Search code examples
c++headerexecutionseqpar

Problem with using header of <execution> in c++


I have gcc 10.2.0 on windows 10. so, is implemented in this latest version of gcc.
The problem is when I copy the example code from the following link :https://docs.w3cub.com/cpp/algorithm/reduce, it says: std::execution has not been declared. Any idea?

#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";
    }
}

Solution

  • Many compilers default to older C++ modes for backwards compatability. Enable C++17 in the settings to use newer features.