Tutorial :Using typedef from inside a template as template argument type


I'm trying to do something like this (completely synthetic example, because the real code is a bit to convoluted):

enum MyInfoType  {      Value1, Value2  };    template<typename T> struct My_Type_Traits  {};    template<> struct My_Type_Traits<int>  {      typedef MyInfoType InfoType;  };    template<typename T>  class Wrap  {       template<My_Type_Traits<T>::InfoType INFO> int GetInfo()       {...}  };    ...  Wrap<int> w;  int info = w.GetInfo<Value1>();  

So basically I'm trying to use a typedef from inside another struct as type of a template parameter. With this code however the compiler complains that struct My_Type_Traits<T>::InfoType is not a type. So what do I need to do to make this work?


You need to use the typename keyword: like typename My_Type_Traits<T>::InfoType to let the compiler know you're referring to a nested type.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »