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]
[Note 2: 
A template-declaration can appear only as a namespace scope or class scope declaration.
— end note]
Its declaration shall not be an export-declaration.
In a function template declaration, the unqualified-id of the declarator-id shall be a name.
[Note 3: 
A class or variable template declaration of a simple-template-id declares a partial specialization ([temp.spec.partial]).
— 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 specialization (explicit or implicit) of one template is distinct from all specializations of any other template.
A template, an explicit specialization ([temp.expl.spec]), and a partial specialization shall not have C language linkage.
[Note 4: 
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 obey the one-definition rule ([basic.def.odr]).
— end note]
[Note 5: 
A template cannot have the same name as any other name bound in the same scope ([basic.scope.scope]), except that a function template can share a name with non-template functions ([dcl.fct]) and/or function templates ([temp.over]).
Specializations, including partial specializations ([temp.spec.partial]), do not reintroduce or bind names.
Their target scope is the target scope of the primary template, so all specializations of a template belong to the same scope as it does.
— end note]
An entity is templated if it is
[Note 6: 
A local class, a local or block variable, or a friend function defined in a templated entity is a templated entity.
— end note]
A templated function is a function template or a function that is templated.
A templated class is a class template or a class that is templated.
A templated variable is a variable template or a variable that is templated.
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 7: 
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.