13 Templates [temp]

13.1 Preamble [temp.pre]

A template defines a family of classes, functions, or variables, an alias for a family of types, or a concept.
[Note 1:
The > token following the template-parameter-list of a template-declaration can be the product of replacing a >> token by two consecutive > tokens ([temp.names]).
— end note]
The declaration in a template-declaration (if any) shall
  • declare or define a function, a class, or a variable, or
  • define a member function, a member class, a member enumeration, or a static data member of a class template or of a class nested within a class template, or
  • define a member template of a class or class template, or
  • be a deduction-guide, or
  • be an alias-declaration.
A declaration introduced by a template declaration of a variable is a variable template.
A variable template at class scope is a static data member template.
[Example 1: template<class T> constexpr T pi = T(3.1415926535897932385L); template<class T> T circular_area(T r) { return pi<T> * r * r; } struct matrix_constants { template<class T> using pauli = hermitian_matrix<T, 2>; template<class T> constexpr static pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } }; template<class T> constexpr static pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } }; template<class T> constexpr static pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } }; }; — end example]
A template-declaration can appear only as a namespace scope or class scope declaration.
Its declaration shall not be an export-declaration.
In a function template declaration, the last component of the declarator-id shall not be a template-id.
[Note 2:
In a class template declaration, if the class name is a simple-template-id, the declaration declares a class template partial specialization.
— end note]
In a template-declaration, explicit specialization, or explicit instantiation the init-declarator-list in the declaration shall contain at most one declarator.
When such a declaration is used to declare a class template, no declarator is permitted.
A template name has linkage.
Specializations (explicit or implicit) of a template that has internal linkage are distinct from all specializations in other translation units.
A template, a template explicit specialization, and a class template partial specialization shall not have C linkage.
Use of a linkage specification other than "C" or "C++" with any of these constructs is conditionally-supported, with implementation-defined semantics.
Template definitions shall obey the one-definition rule.
[Note 3:
Default arguments for function templates and for member functions of class templates are considered definitions for the purpose of template instantiation ([temp.decls]) and must also obey the one-definition rule.
— end note]
A class template shall not have the same name as any other template, class, function, variable, enumeration, enumerator, namespace, or type in the same scope ([basic.scope]), except as specified in [temp.class.spec].
Except that a function template can be overloaded either by non-template functions ([dcl.fct]) with the same name or by other function templates with the same name ([temp.over]), a template name declared in namespace scope or in class scope shall be unique in that scope.
An entity is templated if it is
[Note 4:
A local class, a local variable, or a friend function defined in a templated entity is a templated entity.
— end note]
A template-declaration is written in terms of its template parameters.
The optional requires-clause following a template-parameter-list allows the specification of constraints ([temp.constr.decl]) on template arguments ([temp.arg]).
The requires-clause introduces the constraint-expression that results from interpreting the constraint-logical-or-expression as a constraint-expression.
[Note 5:
The expression in a requires-clause uses a restricted grammar to avoid ambiguities.
Parentheses can be used to specify arbitrary expressions in a requires-clause.
[Example 2: template<int N> requires N == sizeof new unsigned short int f(); // error: parentheses required around == expression — end example]
— end note]
A definition of a function template, member function of a class template, variable template, or static data member of a class template shall be reachable from the end of every definition domain ([basic.def.odr]) in which it is implicitly instantiated ([temp.inst]) unless the corresponding specialization is explicitly instantiated ([temp.explicit]) in some translation unit; no diagnostic is required.