Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation error while using reduce() and std::execution in C++

Tags:

c++

c++17

I am trying to implement a simple use-case reduce() and std:: execution. I am using C++ 17 standard for the following code. However, the following code still does not get compiled.

#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#include <execution>

int main() {
    std:: vector<int> vec(10);
    std:: iota(std::begin(vec), std::end(vec), 1);
    const auto result = std::reduce(std::execution::par, std::begin(vec),
            std::end(vec));
    std:: cout << result;
    return 0;
}

Error:

C:\Users\Blind1729\Desktop>g++ --std=c++17 a.cpp
a.cpp: In function 'int main()':
a.cpp:8:25: error: 'reduce' is not a member of 'std'
    const auto result = std::reduce(std::execution::par, std::begin(vec),
                        ^
a.cpp:8:42: error: 'std::execution' has not been declared
    const auto result = std::reduce(std::execution::par, std::begin(vec),
like image 359
Arun Suryan Avatar asked Oct 30 '25 11:10

Arun Suryan


1 Answers

You need to link against TBB, as it is a dependence of the parallel STD (see C++17 STL Parallel Algorithms - with GCC 9.1 and Intel TBB on Linux and macOS):

g++ --std=c++17 a.cpp -ltbb

And make sure you have at least GCC 9.1 installed.

like image 116
Philipp Claßen Avatar answered Nov 02 '25 04:11

Philipp Claßen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!