C++ Reference ยท Keywords and Statements
The typename keyword
Reference for C++ typename: declaring type parameters, disambiguating dependent names such as T::value_type, and the related template disambiguator.
typename has two jobs inside templates. In a template parameter list it declares a type parameter, exactly like class: template <typename T>. Inside a template body it tells the compiler that a dependent name (a qualified name that depends on a template parameter) names a type.
The second job exists because the compiler cannot look inside T while parsing the template. For T::iterator it has no way to know whether iterator is a type, a static data member or a function until T is known, so by default it assumes it is not a type. Writing typename T::iterator removes the doubt.
template <typename C> void f(const C& c) { typename C::value_type first = *c.begin(); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template <typename T> ... // type parameter typename T::nested-name // dependent type name typename T::template Member<int> // dependent template-id naming a type template <template <typename> typename C> // template template parameter (typename allowed since C++17)
| Where | Meaning |
|---|---|
template <typename T> |
Declares a type parameter. Interchangeable with class. |
typename Q::name |
Tells the compiler Q::name is a type. Needed when Q depends on a template parameter and the name is used where a type is expected. |
obj.template f<X>() |
Tells the compiler that f is a member template, so that < starts an argument list instead of meaning less-than. |
Usage notes
typenameis required only for qualified dependent names that are types, and not where the context already makes a type certain: base-class lists and member-initializer lists never take it.- Since C++20
typenameis optional in more places where only a type can appear: an alias declaration, a member declaration, a trailing return type, the return and parameter types of a function declaration, and the target type of a named cast such asstatic_cast. - The disambiguators nest:
typename T::template Rebind<U>::other. - Forgetting it produces errors like "need 'typename' before 'T::iterator' because 'T' is a dependent scope". The fix is always to add the keyword.
std::vector<int>::iteratoris not dependent, so it needs notypename. Only names involving a template parameter do.- Writing
typenamebefore a name that is not a type is an error. autooften removes the need entirely:auto it = c.begin();.- Standard type traits have
_taliases (std::remove_const_t<T>) so you rarely writetypename std::remove_const<T>::typeby hand.
Related
- template
- using
- Type traits
- SFINAE
- Tutorial: Function templates, Type traits