cppreference.com 上的 Concepts TS 示例代码是否错误?

xml*_*lmx 1 c++ standards clang c++-concepts c++20

下面的代码摘自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);
}
Run Code Online (Sandbox Code Playgroud)

然而,它不能用 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.
Run Code Online (Sandbox Code Playgroud)

cppref 的文档是否错误?

Nic*_*las 5

Cppreference 站点记录了 C++ 生态系统中的许多内容。其中一些是标准的一部分,一些是技术规范的一部分。后者在 URL 中都有“实验性”(显然,页面顶部有一个大警告文本框),并且仅在您使用相关 TS 时才应使用。在本例中,采用了 TS 概念,C++20 核心语言功能就是从中采用的。

这两者(概念 TS 和 C++20)有足够的差异,针对其中一种编写的代码极不可能与另一种的编译器兼容。