![](/img/trans.png)
[英]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.