简体   繁体   English

使用 Apple Clang 14.0 编译时,std::ranges::any_of 失败

[英]std::ranges::any_of fails when compiling with Apple Clang 14.0

When I compile my program I get this error: error: no member named 'any_of' in namespace 'std::ranges' .当我编译我的程序时,我得到这个错误: error: no member named 'any_of' in namespace 'std::ranges' However, I do include all the necessary headers (eg algorithm).但是,我确实包含了所有必要的标题(例如算法)。 I use c++20 standard and my compiler version is Apple Clang 14.0.我使用 c++20 标准,我的编译器版本是 Apple Clang 14.0。 Why do I get this error?为什么会出现此错误? I highly appreciate it if someone is able to explain to me the root cause of this.如果有人能够向我解释其根本原因,我将不胜感激。

I tried to go through if there are some issues with the library and my compiler, but could not get an affirmative answer from here either: https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_library_features如果库和我的编译器有问题,我尝试通过 go,但也无法从这里得到肯定的答案: https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_library_features

I first tried to implement this on my own code when I got the error.当我收到错误时,我首先尝试在我自己的代码上实现它。 However, I get the same errors when running the example code from cppreference: https://en.cppreference.com/w/cpp/algorithm/ranges/all_any_none_of但是,从 cppreference 运行示例代码时,我遇到了同样的错误: https://en.cppreference.com/w/cpp/algorithm/ranges/all_any_none_of

Below is the example code I was trying to run (copied from the previous link).下面是我试图运行的示例代码(从上一个链接复制)。

#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <functional>
 
namespace ranges = std::ranges;
 
int main()
{
    std::vector<int> v(10, 2);
    std::partial_sum(v.cbegin(), v.cend(), v.begin());
    std::cout << "Among the numbers: ";
    ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
 
    if (ranges::all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; })) {
        std::cout << "All numbers are even\n";
    }
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2))) {
        std::cout << "None of them are odd\n";
    }
 
    auto DivisibleBy = [](int d)
    {
        return [d](int m) { return m % d == 0; };
    };
 
    if (ranges::any_of(v, DivisibleBy(7))) {
        std::cout << "At least one number is divisible by 7\n";
    }
}

Expected output预计 output

Among the numbers: 2 4 6 8 10 12 14 16 18 20
All numbers are even
None of them are odd
At least one number is divisible by 7

The output my compiler gives me我的编译器给我的 output

src/day04.cpp:16:13: error: no member named 'copy' in namespace 'std::ranges'
    ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
    ~~~~~~~~^
src/day04.cpp:19:9: error: no member named 'all_of' in namespace 'std::ranges'; did you mean 'std::all_of'?
    if (ranges::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
        ^~~~~~~~~~~~~~
        std::all_of
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/all_of.h:26:1: note: 'std::all_of' declared here
all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
^
src/day04.cpp:23:17: error: no member named 'none_of' in namespace 'std::ranges'
    if (ranges::none_of(
        ~~~~~~~~^
src/day04.cpp:31:17: error: no member named 'any_of' in namespace 'std::ranges'
    if (ranges::any_of(v, DivisibleBy(7)))
        ~~~~~~~~^
4 errors generated.


        ~~~~~~~~^

I tried to include the ranges header in the code #include<ranges> , but got the same errors.我试图在代码#include<ranges>中包含ranges header,但得到了相同的错误。

Range version of the <algorithm> library has not been implemented in Apple Clang 14.0. <algorithm>库的范围版本尚未在 Apple Clang 14.0 中实现。 You can check this by going through each versions of Xcode release notes , or by going through the library files.您可以通过浏览每个版本的 Xcode 发行说明或浏览库文件来检查这一点。

Range version of the <algorithm> library has been added to their most recent stable branch of LLVM however, so you can build it yourself if you need it right now(or just wait for future releases).然而, <algorithm>库的范围版本已添加到他们最近的 LLVM 稳定分支中,因此如果您现在需要它,您可以自己构建它(或者等待未来的版本)。 Alternatively, you can install third party compilers like GCC or Clang.或者,您可以安装第三方编译器,如 GCC 或 Clang。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM