概念中的 cv 限定符需要表达式参数列表

cv qualifiers in concept requires expression parameter list

我有以下使用概念的代码:

struct bar {
    void foo() {}
};

template <typename T>
concept Fooable = requires (const T& t) { // const bar& t doesn't support t.foo()
    { t.foo() };
};

template <typename Fooable>
void callfoo(Fooable bar)
{
    bar.foo();
}

int main()
{
    bar b;
    callfoo(b);

    return 0;
}

我希望代码不会编译,因为 bar 不支持在 const 实例上调用 foo()。但是它确实编译得很好 - link.

cppreference 上的参数列表描述在这方面没有多大帮助:

parameter-list - a comma-separated list of parameters like in a function declaration, except that default arguments are not allowed and it cannot end with an ellipsis (other than one signifying a pack expansion). These parameters have no storage, linkage or lifetime, and are only used to assist in specifying requirements. These parameters are in scope until the closing } of the requirement-seq.

我是否误解了 cv 限定符在 requires 表达式参数列表中的作用?我完全错过了什么吗?我对 cppreference 有一些使用通用参考参数的示例感到更加困惑,因此其中一定有一些意义。

我正在使用 gcc9-fconcepts(尽管 gcc trunk 和 clang 以及 godbolt 上的概念也适用于这段代码,所以不要假设它与编译器相关)。

您的 callfoo 函数中存在错误。您使用的是类型名称,而不是概念。将声明更改为

template <Fooable Foo>
void callfoo(Foo bar)

void callfoo(Fooable auto bar)