0

Hi I'm a beginer studying C++, I'm learning templates and saw some code like this :

template <class T>
class MyClass
{
   typename T::SubType m_var;
};

I was wondering what's the difference between that and this code :

template <class T>
class MyClass
{
   T m_var;
};

why did they use the keyword "typename" if I can just declare my attribute without it? and if my class attribute's name is "m_var" what's the point of "T::SubType"? is that another way to call the attribute ?

I tried to instantiate an object from class MyClass and got this error : error: β€˜int’ is not a class, struct, or union type

ab dg
  • 1
  • what's the difference between `Type` and `Type::SubType`? any guesses? – user253751 May 17 '23 at 17:28
  • `SubType` is supposed to be a member type of the class type `T`, i.e. a `using`/`typedef` alias inside the class that is passed for `T`. The template expects that a class type is passsed for `T` and that it has the member type. Anything else will fail. Why the `typename` is required for this is explained in the linked duplicate. It is not required if you intent to declare a member of type `T` instead of `T::SubType`. – user17732522 May 17 '23 at 17:35

0 Answers0