Ever wonder why you need to specify typename
when you use nested dependent type name? Consider following example without typename
1 | template<typename C> |
This looks like we’re declaring x
as a local variable that’s a pointer to a C::const_iterator
. But it looks that way only because we “know” that C::const_iterator
is a type. But what if C::const_iterator
weren’t a type? What if C
had a static data member that happened to be named const_iterator
, and what if x
happened to be the name of a global variable? In that case, the code above wouldn’t declare a local variable, it would be a multiplication of C::const_iterator
by x
!
This is exactly why typename
is needed before nested dependent type - to avoid ambiguity. Now let’s look at two special cases that we don’t need typename
, because it won’t have such ambiguity.
1 | template<typename T> |
A practical way of specifying long nested type name is by typedef
it
1 | template<typename IterT> |
Last point of typename
: when declaring template parameters, class
and typename
are interchangeable.