c++ - 概念上的cv限定词需要表达式参数列表

标签 c++ c++20 c++-concepts

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

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()。但是,它会编译fine-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限定词?我完全想念什么吗?我对cppreference感到困惑,其中有一些使用通用引用参数的示例,因此其中必须有一些要点。

我正在将gcc9-fconcepts一起使用(尽管gcc主干和带有Godbolt概念的clang也适用于此代码,所以不要以为它与编译器相关)。

最佳答案

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

template <Fooable Foo>
void callfoo(Foo bar)

要么
void callfoo(Fooable auto bar)

关于c++ - 概念上的cv限定词需要表达式参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60366737/

相关文章:

c++ - 静态断言类型 A 可以从类型 B 构造

c++ - 当有多个查询时,检查某些子数组是否已排序的有效方法是什么?

c++ - 我的 QT 设计器插件找不到外部组件

c++ - 从一系列字符构建 string_view

c++ - 如果 std::vector::insert(pos, value) 的位置无效怎么办?

c++ - std::swap of std::priority_queue with lambda comparator 在 C++20 但不是 C++17 下编译: "error: object of type value_compare cannot be assigned"

c++ - 不受约束的 requires-expression 参数

c++ - 如何定义任意 std::vector 满足的概念?

c++ - 安全的多线程计数器增量

c++ - 在回调中更新数据