How to access a template type defined in base class from a derived template class?

53 views Asked by At

Here is my code:

// Here can be more types of base classes and they all define `InsideType`.
struct BaseClass
{
    template <class T>
    struct InsideType
    {
    };
};

// The subclass can inherit from one type of base class. 
template <class Base>
struct SubClass : public Base
{
    // Compile-Error on Visual Studio 2022 C++20:
    //   error C2059: syntax error: '<'
    //   error C2238: unexpected token(s) preceding ';'
    typename Base::InsideType<int> val;
};

int main()
{
    SubClass<BaseClass> var;
    return 0;
}

I want to access InsideType from the derived template class SubClass. But typename seems to only work when InsideType is not a template type.

The compile errors do not make sense on Visual Studio 2022 C++20:

// error C2059: syntax error: '<'
// error C2238: unexpected token(s) preceding ';'
typename Base::InsideType<int> val;

Is there any way to do this? Thanks.

1

There are 1 answers

0
Vlad from Moscow On

Try the following

template <class Base>
struct SubClass : public Base
{
    // Compile-Error on Visual Studio 2022 C++20:
    //   error C2059: syntax error: '<'
    //   error C2238: unexpected token(s) preceding ';'
    typename Base:: template InsideType<int> val;
};