florestan
florestan

Reputation: 4655

Error with Clang 15 and C++20 `std::views::filter`

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++:

Compiler Explorer.

Upvotes: 5

Views: 3264

Answers (1)

user17732522
user17732522

Reputation: 76688

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.

Upvotes: 8

Related Questions