我有以下使用概念的代码:
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/