c++ - 在 C++ 中使用 reduce() 和 std::execution 时出现编译错误

标签 c++ c++17

我正在尝试实现一个简单的用例 reduce()std::execution。我正在为以下代码使用 C++ 17 标准。但是,下面的代码仍然没有编译。

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

错误:

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),

最佳答案

您需要针对 TBB 进行链接,因为它依赖于并行 STD(请参阅 C++17 STL Parallel Algorithms - with GCC 9.1 and Intel TBB on Linux and macOS ):

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

并确保您至少安装了 GCC 9.1。

关于c++ - 在 C++ 中使用 reduce() 和 std::execution 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61564006/

相关文章:

c++ - 如何查看c++程序生成的.dat文件?

c++ - 返回指向嵌套 std::vector 项的指针的最佳方法

c++ - 你能从 `volatile const char*` 构造一个字符串吗? (不使用 `const_cast` )

c++ - if constexpr 用于可变长度元素 Get<>

c++ - 三重与号 `&&&` 在 C++ 中代表什么?

c++ - 为什么不能将 `decltype()` 用于 lambda 的 `std::function<>`?

c++ - 是否允许复制/移动省略以使使用已删除函数的程序格式正确?

c++ - 为什么 set/get_default_resource 使用指针而不是引用?

c++ - 如何将 std::visit 与包含枚举的 std::variant 一起使用

c++ - 禁止跨编译单元包含不兼容的 header