Why does the word “typedef” need “typename” after it for dependent types?

Dependent types typically need typename to tell the compiler that it is a type, not a function or variable.

However, this is not always the case. For example, the base class does not require this, because it can only be a type ever:

 template<class T> struct identity { typedef T type; } template<class T> class Vector : identity<vector<T> >::type { }; // no typename 

Now my question is: why does typedef ever require typename after it ?

 template<class T> class Vector { typedef typename /* <-- why do we need this? */ vector<T>::iterator iterator; }; 
+8
c ++ typedef typename
Feb 07 '14 at 10:22
source share
1 answer

typedef does not need to appear before the type.

 template <typename T> struct S { typedef T type; }; template <typename T> void f() { typename S<T>::type typedef t; } 

This is perfectly true, and in this case, I hope you understand that parsing will be difficult if typename are optional.

I understand that

 template <typename T> void f() { typedef S<T>::type t; } 

can be interpreted in different ways, but this would lead to unexpected cases when the position of the typedef keyword suddenly becomes significant.

+7
Feb 07 '14 at 10:31
source share



All Articles