How to use nested type of template base class in sub class?

57 views Asked by At

I have code like this:

template <typename T>
struct bar
{
        typedef T type;
};

template <typename T>
struct sub_bar : bar<T>
{
        typename bar<T>::type x;
};

I know that this is recommended way, but I want write code like this:

template<typename T>

struct sub_bar : bar<T>

{
        ...

        type x;

};

How to do it? I think I can do something like this:


template<typename T>

struct sub_bar : bar<T>

{
        typedef typename bar<T>::type type;

        type x;

};

Is it ambiguous what's type? However this code works with gcc9 compiler.

1

There are 1 answers

0
Davis Herring On

I would tend to use a trick of forcing qualified lookup and write

using typename sub_bar::type;

to avoid repeating the template arguments of the base class (and to avoid potential ambiguity in a strange situation where both classes were searched for type), but what you wrote definitely works.