[temp.pre] (original) (raw)
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 thetemplate-parameter-list of atemplate-declarationcan be the product of replacing a>> token by two consecutive >tokens ([temp.names]).
— _end note_]
Thedeclarationin atemplate-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 avariable is a variable template.
A variable template at class scope is astatic 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_]
Atemplate-declarationcan appear only as a namespace scope or class scope declaration.
In a function template declaration, the last component of thedeclarator-idshall not be atemplate-id.
In atemplate-declaration, explicit specialization, or explicit instantiation theinit-declarator-listin the declaration shall contain at most one declarator.
When such a declaration is used to declare a class template, no declarator is permitted.
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, withimplementation-defined semantics.
[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 templatedif it is
- a template,
- an entity defined ([basic.def]) or created ([class.temporary]) in a templated entity,
- a member of a templated entity,
- an enumerator for an enumeration that is a templated entity, or
- the closure type of a lambda-expression ([expr.prim.lambda.closure]) appearing in the declaration of a templated entity.
[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 atemplate-parameter-list allows the specification of constraints ([temp.constr.decl]) on template arguments ([temp.arg]).
[Note 5:
The expression in a requires-clauseuses 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(); — _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.