Home > Back-end >  How to inherit using declarations from base class
How to inherit using declarations from base class

Time:12-04

I declare types within class Config, pass this to base class Parent so Child can access.

The idea is each Child (there are many) won't have to keep declaring it's own using declarations, because they are already in Parent.

However, this doesn't compile. Child cannot see Parent::Type.

Is it possible to achieve this somehow?

template<class CONFIG>
struct Parent
{
    using Type = typename CONFIG::Type;
    // Plus a lot more types....
};

template<class CONFIG>
struct Child : public Parent<CONFIG>
{
    void x(Type p){}     // Compiler error. Cannot see Parent::Type
};

struct Config
{
    using Type = int; 
    // Plus a lot more types....
};

int main() 
{
    Child<Config> c;
    return 0;
}

CodePudding user response:

These are automatically inherited, but not visible. The reason these are not visible is, template base class is not considered during ADL. It's not specific to types, same occurs with member variables. If you want them to be accessible, you need to bring them in scope:

struct Child : public Parent<CONFIG>
{
    using Base = Parent<Config>;
    using typename Base::Type;
    void x(Type p){}     // Compiler error. Cannot see Parent::Type
};
  • Related