Search code examples
c++templatesinheritancetypedef

Inheritance of public typedef with templates


I am trying to access a public typedef defined in the base (A) class from the inherited class (B).

But I still need to qualify the typename with A.

Is there a syntax to explicitely make mytype available to B without resorting to redundant trickery ?

template <typename T>
class A {
public:
        typedef T mytype;
};

template <typename T>
class B : public A<T> {
public:
        /* This seems redundant */
        typename A<T>::mytype foo(typename A<T>::mytype x) {
                return x;
        }

        /* This is also redundant but would make bar below work*/
        // typedef typename A<T>::mytype mytype;

        /* This feels natural but does not work */
        mytype bar(mytype x) {
                return x;
        }
};

Solution

  • The reason for this is that unqualified lookup doesn’t find members of dependent base classes.
    One solution would be to make the lookup qualified:

    typename B::mytype
    

    In order not to type this every time you can introduce an alias into B:

    using mytype = typename B::mytype;