4

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.

florestan
  • 4,405
  • 2
  • 14
  • 28
  • _"Is the code incorrect?"_ - No it looks correct to me. – Ted Lyngmo Oct 02 '22 at 20:29
  • Some supporting information: According to cppreference's [C++20 Compiler Support Page](https://en.cppreference.com/w/cpp/compiler_support/20) The One Ranges Proposal (ranges) is still only partially supported by GCC and Clang. – oraqlle Oct 03 '22 at 01:29

1 Answers1

7

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.

Update: Both of these issues have been resolved on current LLVM trunk. The linked bug reports regarding libstdc++'s views implementation have been fixed. The libc++ views implementation (at least for the code in the question) is no longer guarded with -fexperimental-library. So I would expect that the LLVM 16 release will work with the shown code fine.

user17732522
  • 53,019
  • 2
  • 56
  • 105