10

I am testing ranges from C++ 20 , and this is my main.cpp:

#include <ranges>
#include <iostream>

int main()
{
    auto const ints = {0,1,2,3,4,5};
    auto even = [](int i) { return 0 == i % 2; };
    auto square = [](int i) { return i * i; };

    for (int i : ints | std::views::filter(even) | std::views::transform(square)) {
        std::cout << i << ' ';
    }

    std::cout << '\n';
}

To compile it with clang++-12 but it couldn't find 'ranges':

$ clang++-12 --version
Ubuntu clang version 12.0.1-++20210525082622+328a6ec95532-1~exp1~20210525063352.95
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin


$ clang++-12 main.cpp
cpp_ranges.cpp:1:10: fatal error: 'ranges' file not found
#include <ranges>
         ^~~~~~~~
1 error generated.

to call clang++12 with --std=c++20 doesn't make a difference.

How could I fix it?

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
Rahn
  • 4,787
  • 4
  • 31
  • 57
  • 6
    It seems that clang only has partial support for ranges, and only in version 13: https://en.cppreference.com/w/cpp/compiler_support – Yksisarvinen May 26 '21 at 09:09
  • 5
    Are you using libc++? The version of clang isn't relevant to ranges support, if you're on Linux I think clang defaults to using your system version of libstdc++ – Alan Birtles May 26 '21 at 09:15
  • @AlanBirtles I'm using Linux, but have never heard about `libstdc++`... – Rahn May 26 '21 at 09:49
  • https://gcc.gnu.org/onlinedocs/libstdc++/ – Alan Birtles May 26 '21 at 09:52
  • 4
    @rahn C++ defines the C++ standard library, which has multiple implementations. The most used ones are libstdc++ (provided by GNU), libc++ (provided by LLVM), and Microsoft STL. On a Linux system, typically, libstdc++ is used by default even with a Clang compiler. Which might be your case. I don't know of any sophisticated check, but you can simply print `sizeof(std::string)`. With libstdc++, it gives 32, while with libc++, it is 24. Or, you can `ldd` your program file. – Daniel Langr May 26 '21 at 09:55

0 Answers0