[英]How to call function in derived if exists else use a default if using CRTP?
[英]How to use using with CRTP?
在创建没有其他功能的类型时,我尝试使用using
而不是子类或使用typedef
。
我有一个CRTP层次结构,我试图在树上传播具体类型。
GrandKid
似乎编译得很好。 有没有办法让GrandKid_2
工作?
错误信息
junk.cpp:18:26: error: ‘GrandKid_2’ was not declared in this scope
码
template<typename T>
struct Parent
{
};
template<typename T>
struct Child
: public Parent<T>
{
};
struct GrandKid :
public Child<GrandKid>
{
};
// using GrandKid_2 = Child<GrandKid_2>; // doesn't compile
int
main( int argv, char* argc[] )
{
GrandKid gk; // ok
}
像typedef
一样using
创建别名,而不是新类型。 因此,您无法使用GrandKid_2来定义自己。
您肯定需要创建一个新类型:
struct GrandKid_2 : Child<GrandKid_2> {};
顺便问一下,你真的希望 GrandKid_2
成为自己的孩子吗?也许你的意思是 using GrandKid_2 = Child<GrandKid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.