1

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.

czs108
  • 151
  • 4
  • 1
    You want to type `Base::template InsideType`. – Drew Dormann Mar 21 '23 at 11:24
  • @DrewDormann, Thank you so much, I found its details according to your answer: https://en.cppreference.com/w/cpp/language/dependent_name, *The template disambiguator for dependent names*. – czs108 Mar 21 '23 at 11:34

1 Answers1

1

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;
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335