Search code examples
c++c++20apple-clang

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'. However, I do include all the necessary headers (e.g. algorithm). I use c++20 standard and my compiler version is 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

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

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

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

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.


Solution

  • Range version of the <algorithm> library has not been implemented in Apple Clang 14.0. You can check this by going through each versions of Xcode release notes, or by going through the library files.

    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). Alternatively, you can install third party compilers like GCC or Clang.