is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type. In the original C++ compilers before the first ISO standard was completed, the <code>typename</code> keyword was not part of the C++ language and Bjarne Stroustrup used the <code>class</code> keyword for template arguments instead. While <code>typename</code> is now the preferred keyword, older source code may still use the <code>class</code> keyword instead (for example see the difference in source code examples between The Design and Evolution of C++ by Bjarne Stroustrup published in 1994 and the source code examples in The C++ Programming Language: Fourth Edition by Bjarne Stroustrup published in 2013).
In C++'s generic programming feature known as "templates", <code>typename</code> can be used for introducing a template parameter:
An alternative and semantically equivalent keyword in this scenario is "<code>class</code>":
When using concepts, a means of constraining the generic type to satisfy some set of rules, the name of the concept replaces the keyword <code>typename</code>.
Consider this invalid code:
This code looks like it should compile, but it is incorrect because the compiler does not know if <code>T::Bar</code> is a type or a value. The reason it doesn't know is that <code>T::Bar</code> is a "template-parameter dependent name", or "dependent name" for short, which then could represent anything named "<code>Bar</code>" inside a type passed to <code>foo()</code>, which could include typedefs, enums, variables, etc.
To resolve this ambiguity, the C++ Language Standard declares: <blockquote>A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword <code>typename</code>.</blockquote> In short, if the compiler can't tell if a dependent name is a value or a type, then it will assume that it is a value. In our example, where <code>T::Bar</code> is the dependent name, that means that rather than declaring a pointer to <code>T::Bar</code> named <code>p</code>, the line
will instead multiply the "value" <code>T::Bar</code> by <code>p</code> (which is nowhere to be found) and throw away the result. The fact that in <code>UsesBar</code> the dependent <code>Bar</code> is in fact a type does not help since <code>foo()</code> could be compiled long before <code>UsesBar</code> is seen. Furthermore, if there is also a class like:
then the compiler would be obliged to interpret the <code>T::Bar</code> in <code>foo()</code> as an access to data member <code>HoldsBar::Bar</code> when instantiated. But since <code>Bar</code> is not a static data member it will flag an error.
The solution to this problem is to explicitly tell the compiler that <code>T::Bar</code> is in fact a type. For this, the <code>typename</code> keyword is used:
Now the compiler knows for sure that <code>T::Bar</code> is a type, and will correctly make <code>p</code> a pointer to an object of that type.