문제

When creating types with no additional features, I try to use using, rather than subclassing or using typedef.

I have a CRTP hierarchy where I am trying to propagate the concrete type up the tree.

GrandKid seems to compile fine. Is there a way to get GrandKid_2 to work?

ERROR MESSAGE

junk.cpp:18:26: error: ‘GrandKid_2’ was not declared in this scope

CODE

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
}
도움이 되었습니까?

해결책

using, like typedef, creates an alias, not a new type. Therefore, you cannot use GrandKid_2 to define itself.

You will surely need to create a new type :

struct GrandKid_2 : Child<GrandKid_2> {};

By the way, do you really want GrandKid_2 to be its own child ? Maybe you meant using GrandKid_2 = Child<GrandKid>

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top