C++20 and pipe operator

Hi all,

in a bigger project, clang++ 14.0.6 (with -std=c++20) generates error messages for the C++20 pipe operator that g++ 12.1.0 (with -std=c++20) does not generate.

These problems even arise with a simple test program like the following from a textbook.

#include <iostream>
#include <ranges>
#include <vector>

int main()
{
    std::vector numbers{ 1, 2, 3, 4, 5, 6 };

    auto results = numbers //
                   | std::views::filter([](auto const n) { return 0 == n % 2; }) //
                   | std::views::transform([](auto const n) { return n * n; }) //
                   ;

    for (auto v : results)
        std::cout << v << " ";
    std::cout << '\n';

    return 0;
}

g++ compiles the given source code well, and produces the correct output “4 16 36”, while clang++ generates errors like

error: no matching function for call to '__begin'
error: constraints not satisfied for alias template 'sentinel_t'
error: invalid operands to binary expression

Can anyone enlighten me, where the problem is with that code?

Clang sometimes chokes when using newer libstdc++ versions.

If you use libc++ instead, it works: Compiler Explorer

Well, I had to install libc++ on my up-to-date arch system first, and then got …

error: no member named 'views' in namespace 'std'

for

clang++ -std=c++20 -stdlib=libc++ x.cpp

The trunk version that you used in the compiler explorer has a newer version, right?

Yes, it doesn’t work with 14.0.0 libc++ either: Compiler Explorer