可能只是概念很糟糕,但我不明白为什么.并没有找到任何构造函数的例子.或者它可能与构造函数无关......
template < typename T >
concept bool C_Object() {
return requires {
T();
};
}
template < C_Object Object>
class DefaultManager {
// Content;
};
template < C_Object Derived >
class Component {
// Content
};
struct Test : public Component<Test> {
int data;
Test() = default;
};
int main() {
Test test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出错误:
test2.cpp:21:36: error: template constraint failure
struct Test : public Component<Test> {
^
test2.cpp:21:36: note: constraints not satisfied
test2.cpp:2:14: note: within ‘template<class T> concept bool C_Object() [with T = Test]’
concept bool C_Object() {
^~~~~~~~
test2.cpp:2:14: note: the required expression ‘T()’ would be ill-formed
Run Code Online (Sandbox Code Playgroud)
这听起来像是:"嘿,我的代码坏了,请修理它",对不起.
不管怎样,谢谢
祝你有美好的一天
问题出在这里:
struct Test : public Component<Test> {
Run Code Online (Sandbox Code Playgroud)
无论何时命名约束类模板的特化,都会根据约束验证给定的参数.在这种特殊情况下,这意味着C_Object<Test>检查是否满意,但由于Test不完整 - 编译器尚未解析其定义 - C_Object不满意.
这是CRTP基础的经典问题的"概念"版本:您必须延迟检查派生类,直到其定义完成.