小编lem*_*age的帖子

使用 GNU G++17 和 Clang++ 17/20 编译器的 C++17 中的“std::reduce()”方法出现错误

std::reduce()我在使用C++17 中的方法与 GNU G++17 和 Clang++ 17/20 编译器时遇到错误。尽管包含<numeric>应提供对reduce()方法的访问的标头,但编译器报告该方法未在当前作用域中声明。但是,当我切换到 GNU G++20 编译器时,代码编译时没有任何问题。

代码示例:

#include <iostream>
#include <numeric>

int main() {
    int arr[] = {1, 2, 3, 4, 5};

    // Attempting to use std::reduce() method
    int sum = std::reduce(std::begin(arr), std::end(arr), 0);

    std::cout << "Sum: " << sum << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误消息(编译器:GNU C++17 7.3.0):

program.cpp: In function 'int main()':
program.cpp:8:20: error: 'reduce' is not a member of 'std'
     int sum = std::reduce(std::begin(arr), std::end(arr), 0);
                    ^~~~~~
Run Code Online (Sandbox Code Playgroud)

错误消息(编译器:Clang++ …

c++ gcc clang c++17

2
推荐指数
1
解决办法
249
查看次数

标签 统计

c++ ×1

c++17 ×1

clang ×1

gcc ×1