Home > Blockchain >  Define type alias between template and class declaration in order to inherent from it
Define type alias between template and class declaration in order to inherent from it

Time:05-21

I have a class template which uses some type alias through its implementation, and also inherits from the same type:

template<typename TLongTypename, 
         typename TAnotherLongTypename, 
         typename THeyLookAnotherTypename>
class A : SomeLongClassName<TLongTypename, 
                           TAnotherLongTypename,
                           THeyLookAnotherTypename>
{
    using Meow = SomeLongClassName<TLongTypename, 
                                   TAnotherLongTypename, 
                                   THeyLookAnotherTypename>;
    
    // ... (using Meow a lot)
};

Naturally I want to inherit from the type alias Meow and not from the long name. Is there any nice way to do that, hopefully such that Meow will be defined inside the template scope but before the class scope?

CodePudding user response:

You could declare the type alias Meow as a default template parameter, directly in the template parameter list, which means you only have to spell it out once:

template<typename TLongTypename, 
         typename TAnotherLongTypename, 
         typename THeyLookAnotherTypename,
         // declare Meow once here 
         typename Meow = SomeLongClassName<TLongTypename, 
                                           TAnotherLongTypename,
                                           THeyLookAnotherTypename>>
class A : Meow   // inherit
{
    Meow X;   // use
};
  • Related