Typename
   HOME

TheInfoList



OR:

"typename" is a keyword in the C++
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
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 typename keyword was not part of the C++ language and
Bjarne Stroustrup Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist, known for the development of the C++ programming language. He led the Large-scale Programming Research department at Bell Labs, served as a professor of computer sci ...
used the class keyword for template arguments instead. While typename is now the preferred keyword, older source code may still use the class 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).


A synonym for "class" in template parameters

In C++'s
generic programming Generic programming is a style of computer programming in which algorithms are written in terms of data types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneer ...
feature known as " templates", typename can be used for introducing a template
parameter A parameter (), generally, is any characteristic that can help in defining or classifying a particular system (meaning an event, project, object, situation, etc.). That is, a parameter is an element of a system that is useful, or critical, when ...
: // Define a generic function that returns the greater of its two arguments template const T& max(const T& x, const T& y) An alternative and semantically equivalent keyword in this scenario is "class": // Define a generic function that returns the greater of its two arguments template const T& max(const T& x, const T& y)


A method for indicating that a dependent name is a type

Consider this invalid code: template void foo(const T& t) struct StructWithBarAsType ; int main() This code looks like it should compile, but it is incorrect because the compiler does not know if T::bar is a type or a value. The reason it doesn't know is that T::bar is a "template-parameter dependent name", or "dependent name" for short, which then could represent anything named "bar" inside a type passed to foo(), which could include
typedef typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (''alias'') for another data type, but does not create a new type, except in the obscure case of a qualified typedef of ...
s, enums, variables, etc. To resolve this ambiguity, the C++ Language Standard declares:
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 typename.
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 T::bar is the dependent name, that means that rather than declaring a pointer to T::bar named p, the line T::bar * p; will instead multiply the "value" T::bar by p (which is nowhere to be found) and throw away the result. The fact that in StructWithBarAsType the dependent bar is in fact a type does not help since foo() could be compiled long before StructWithBarAsType is seen. Furthermore, if there is also a class like: struct StructWithBarAsValue ; then the compiler would be obliged to interpret the T::bar in foo() as an access to data member StructWithBarAsValue::bar when instantiated. But since bar is not a static data member it will flag an error. The solution to this problem is to explicitly tell the compiler that T::bar is in fact a type. For this, the typename keyword is used: template void foo(const T& t) Now the compiler knows for sure that T::bar is a type, and will correctly make p a pointer to an object of that type.


See also

* Argument-dependent name lookup – another C++ name lookup rule


References

{{reflist C++