typename vs class (Visual Studio 2010)


Under what circumstances does NOT ‘class’ replace ‘typename’?

struct C
{
class CC{};
typedef CC CC2;
};
template<class T> //using 'class' is fine
struct D
{
typename T::CC2 c1; //'typename' cannot be prelaced by 'class'
class T::CC c2; //using 'class' is fine
//T::CC c3; //error: 'T::CC' : dependent name is not a type
//class T::CC2 c4; //error C2242: typedef name cannot follow class/struct/union
//T::CC2 c5; //error: 'T::CC2' : dependent name is not a type
};
D<C> a;

Summary: if the subtype if not typedef-ed, class can do the job of typename.
Otherwise, it has to be typename.

Advertisements

What if always use class for typename?


Anything wrong with this template class?

template <typename T>
class C
{
 typedef class T::SubType Subtype2;
};

It compiles fine on VS2011 without extenstion. This makes me wonder if keyword ‘typename’ can be removed from MSVB leaving only ‘class’ there.
Please let me know when typename HAS to be used.