Search code examples
c++clangstandardsc++20c++-concepts

Is this sample code for Concepts TS on cppreference.com wrong?


The code below is excerpted from cppref:

#include <string>

using namespace std::literals;

template<typename T>
concept bool EqualityComparable = requires(T a, T b)
{
    {
        a == b
    }
    ->bool;
};

void f(EqualityComparable&&) {}

int main()
{
    f("abc"s);
}

However, it cannot be compiled with clang-10:

[root@mine ~]# clang++ -std=c++20 -stdlib=libc++ main.cpp
main.cpp:6:14: warning: ISO C++20 does not permit the 'bool' keyword after 'concept' [-Wconcepts-ts-compat]
concept bool EqualityComparable = requires(T a, T b)
        ~~~~~^
main.cpp:11:7: error: expected concept name with optional arguments
    ->bool;
      ^
main.cpp:14:8: error: unknown type name 'EqualityComparable'
void f(EqualityComparable&&) {}
       ^
1 warning and 2 errors generated.

Is the documentation of cppref wrong?


Solution

  • The Cppreference site documents many things in the C++ ecosystem. Some of those things are part of the standard, and some of them are parts of technical specifications. The latter all have "experimental" in the URL (and a big warning text box at the top of the page, apparently) and should only be used if you're using the TS in question. In this case, the concepts TS, from which the C++20 core language feature was adopted.

    The two (Concepts TS and C++20) have sufficient differences that code written against one is highly unlikely to be compatible with a compiler for the other.