Home > database >  Error with Clang 15 and C 20 `std::views::filter`
Error with Clang 15 and C 20 `std::views::filter`

Time:10-04

I'm wondering why this code doesn't compile with clang 15, even though the ranges library is marked as fully supported in clang 15 on the compiler support page? It does compile with g 12, even if the support is marked only as partial, and it compiles with clang trunk.

#include <ranges>
#include <vector>

int main() {
    std::vector x{1,2,3,4};
    auto y = x | std::views::filter([](auto i){ return i <2;});
}

Is the code incorrect? And if it is, is there a way to work around the bug until clang 16 gets released. I guess ranges-v3 would work, but perhaps someone knows how to fix it using only the standard library.

Interestingly, I get different errors when using -stdlib=libstdc and -stdlib=libc :

  • libstdc : error: invalid operands to binary expression ('std::vector<int>' and '_Partial<std::ranges::views::_Filter, decay_t<(lambda at <source>:7:37)>>' (aka '_Partial<std::ranges::views::_Filter, (lambda at <source>:7:37)>'))
  • libc : error: no member named 'views' in namespace 'std'

Compiler Explorer.

CodePudding user response:

The code is fine.

However Clang's implementation of concepts is broken in a way that libstdc 's views don't work. This has been a known issue for a while, but apparently has been resolved a few days ago. The code now works on Clang trunk with libstdc . See similar question and relevant bug reports 1 and 2.

Libc 's implementation of views is guarded behind the -fexperimental-library flag with which the particular example in your question also compiles. I am not sure, but I think the implementation is still incomplete, which is why it isn't enabled by default. It could be that my information on that is outdated though, in which case the guard might be there only to wait for stabilization.

  • Related