Sunday, July 1, 2007

understand the two mieanings of typename

names in a template that are dependent on template parameters are called dependent names.

when a dependent name is nested inside a class it is a nested dependent name.

nested dependent type name i.e. a nested dependent name that refers to a type

non dependent names --opposite of this

if the parser encounters a nested dependent name in a template it assumes that the name is not a type unless you tell it otherwise. by default nested dependent typenames are not types.
so u say: typename c::const_iterator iter(container.begin());
the simple general rule is that anytime you refer to a nested dependent typename in a template you must immediately precede it by the word typename

exception to above rule is that typename must not precede nested dependent type names in a list of base classes or as a base class identifier in am ember initialization list.

typedef typename std::iterator_traits::value_type value_type
value_type temp(*iter)

enforcement of the typename rule varies from compiler to compiler.

things to remember:
when declaring template parameters class and typename are interchangeable.
use typenames to idnetify nested dependent type names except in base class lists or as a base class identifier in a member initialization list

1 comment:

mlt said...

Thanks! That helped a lot, because I didn't understand where compiler suggested to insert typename.