TITLE: typedef vs typename in templates (Newsgroups: comp.std.c++, 31 Mar 99) [snip] CLAMAGE: Steve Clamage The keywords "typename" and "typedef" have distinct and unrelated purposes. Neither one is a hint to the compiler. Each has a specific syntactic and semantic role in standard C++. The keyword typedef defines a name to be an alias in the current scope for a previously-declared type. The keyword typename in a template definition is a name qualifier that specifies that a name dependent on a template parameter is the name of some as-yet unspecified type. It does not declare or define the name, nor does it declare or define the type. In standard C++, if a dependent name in a template definition is not qualified with "typename", the name is assumed NOT to be the name of a type. Not all compilers enforce that rule (yet). Originally, C++ did not have the keyword typename. Without knowing whether a name is a type, a template definition cannot be parsed until it is instantiated. Example, where T is a template parameter: T::A * B; If T::A is a type, the line declares B to be a pointer; otherwise it multiplies T::A by B (perhaps for the side effect of an overloaded operator). Consequently, compilers could not do anything with a template definition until it was instantiated. If the line was in error, you would then get the same error message on every instantiation. To allow better error handling and reporting, the "typename" requirement was added. A compiler can now sometimes determine that a construct cannot be valid for any instantiation, issue the error message for the template definition, and not attempt any instantiations. Once "typename" was added to C++, it made sense to allow it to be used in declaring template parameters. The keyword "class" in template ... seems like over-specification when T need not be a class type. template ... is an alternative, more expressive, declaration syntax.