18 KiB
[temp.expl.spec]
13 Templates [temp]
13.9 Template instantiation and specialization [temp.spec]
13.9.4 Explicit specialization [temp.expl.spec]
An explicit specialization of any of the following:
function template
class template
variable template
member function of a class template
static data member of a class template
member class of a class template
member enumeration of a class template
member class template of a class or class template
member function template of a class or class template
can be declared by a declaration introduced bytemplate<>; that is:
explicit-specialization:
template < > declaration
[Example 1: template class stream;
template<> class stream { /* ... / }; // #1template class Array { / ... / };template void sort(Array& v) { / ... / }template<> void sort(Array&); // #2template<> void sort(Array<char>&); // #3 template argument is deduced ([temp.arg.explicit])
Given these declarations, #1 will be used as the definition of streams ofchars; other streams will be handled by class template specializations instantiated from the class template.
Similarly, #2 will be used as the sort function for arguments of type Array and #3 will be used for arguments of typeArray<char*>; otherArray types will be sorted by functions generated from the function template.
â end example]
The declaration in an explicit-specialization shall not be an export-declaration.
An explicit specialization shall not use a storage-class-specifier ([dcl.stc]) other than thread_local.
An explicit specialization may be declared in any scope in which the corresponding primary template may be defined ([dcl.meaning], [class.mem], [temp.mem]).
An explicit specialization does not introduce a name ([basic.scope.scope]).
A declaration of a function template, class template, or variable template being explicitly specialized shall be reachable from the declaration of the explicit specialization.
[Note 1:
A declaration, but not a definition of the template is needed.
â end note]
The definition of a class or class template shall be reachable from the declaration of an explicit specialization for a member template of the class or class template.
[Example 2: template<> class X { /* ... */ }; // error: X not a templatetemplate class X;
template<> class X<char*> { /* ... */ }; // OK, X is a template â end example]
A member function, a member function template, a member class, a member enumeration, a member class template, a static data member, or a static data member template of a class template may be explicitly specialized for a class specialization that is implicitly instantiated; in this case, the definition of the class template shall be reachable from the explicit specialization for the member of the class template.
If such an explicit specialization for the member of a class template names an implicitly-declared special member function ([special]), the program is ill-formed.
A member of an explicitly specialized class is not implicitly instantiated from the member declaration of the class template; instead, the member of the class template specialization shall itself be explicitly defined if its definition is required.
The definition of the class template explicit specialization shall be reachable from the definition of any member of it.
The definition of an explicitly specialized class is unrelated to the definition of a generated specialization.
That is, its members need not have the same names, types, etc. as the members of a generated specialization.
Members of an explicitly specialized class template are defined in the same manner as members of normal classes, and not using the template<> syntax.
The same is true when defining a member of an explicitly specialized member class.
However, template<> is used in defining a member of an explicitly specialized member class template that is specialized as a class template.
[Example 3: template struct A {struct B { }; template struct C { };};
template<> struct A {void f(int);};
void h() { A a; a.f(16); // A::f must be defined somewhere}// template<> not used for a member of an explicitly specialized class templatevoid A::f(int) { /* ... / }template<> struct A::B {void f();};// template<> also not used when defining a member of an explicitly specialized member classvoid A::B::f() { / ... / }template<> template struct A::C {void f();};// template<> is used when defining a member of an explicitly specialized member class template// specialized as a class templatetemplate<>template void A::C::f() { / ... / }template<> struct A::B {void f();};template<> void A::B::f() { / ... / } // error: template<> not permittedtemplate<> template struct A::C {void f();};template void A::C::f() { / ... */ } // error: template<> required â end example]
If a template, a member template or a member of a class template is explicitly specialized, a declaration of that specialization shall be reachable from every use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.
If the program does not provide a definition for an explicit specialization and either the specialization is used in a way that would cause an implicit instantiation to take place or the member is a virtual member function, the program is ill-formed, no diagnostic required.
An implicit instantiation is never generated for an explicit specialization that is declared but not defined.
[Example 4: class String { };template class Array { /* ... / };template void sort(Array& v) { / ... / }void f(Array& v) { sort(v); // use primary template sort(Array&), T is String}template<> void sort(Array& v); // error: specialization after use of primary templatetemplate<> void sort<>(Array<char>& v); // OK, sort<char*> not yet usedtemplate struct A {enum E : T; enum class S : T;};template<> enum A::E : int { eint }; // OKtemplate<> enum class A::S : int { sint }; // OKtemplate enum A::E : T { eT };template enum class A::S : T { sT };template<> enum A::E : char { echar }; // error: A::E was instantiated// when A was instantiatedtemplate<> enum class A::S : char { schar }; // OK â end example]
The placement of explicit specialization declarations for function templates, class templates, variable templates, member functions of class templates, static data members of class templates, member classes of class templates, member enumerations of class templates, member class templates of class templates, member function templates of class templates, static data member templates of class templates, member functions of member templates of class templates, member functions of member templates of non-template classes, static data member templates of non-template classes, member function templates of member classes of class templates, etc., and the placement of partial specialization declarations of class templates, variable templates, member class templates of non-template classes, static data member templates of non-template classes, member class templates of class templates, etc., can affect whether a program is well-formed according to the relative positioning of the explicit specialization declarations and their points of instantiation in the translation unit as specified above and below.
When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.
[Note 2:
A class template explicit specialization that has been declared but not defined can be used exactly like other incompletely-defined classes ([basic.types]).
[Example 5: template class X; // X is a class templatetemplate<> class X;
X* p; // OK, pointer to declared class X X x; // error: object of incomplete class X â end example]
â end note]
[Note 3:
An explicit specialization of a constrained template needs to satisfy that template's associated constraints ([temp.constr.decl]).
The satisfaction of constraints is determined when forming the template name of an explicit specialization in which all template arguments are specified ([temp.names]), or, for explicit specializations of function templates, during template argument deduction ([temp.deduct.decl]) when one or more trailing template arguments are left unspecified.
â end note]
A function with the same name as a template and a type that exactly matches that of a template specialization is not an explicit specialization ([temp.fct]).
Whether an explicit specialization of a function or variable template is inline, constexpr, constinit, or consteval is determined by the explicit specialization and is independent of those properties of the template.
Similarly, attributes and function-contract-specifiers appearing in the declaration of a template have no effect on an explicit specialization of that template.
[Example 6: template void f(T) { /* ... / }template inline T g(T) { / ... / }template<> inline void f<>(int) { / ... / } // OK, inlinetemplate<> int g<>(int) { / ... */ } // OK, not inlinetemplate noreturn void h(maybe_unused int i);template<> void h(int i) {// Implementations are expected not to warn that the function returns// but can warn about the unused parameter.} â end example]
An explicit specialization of a static data member of a template or an explicit specialization of a static data member template is a definition if the declaration includes an initializer; otherwise, it is a declaration.
[Note 4:
The definition of a static data member of a template for which default-initialization is desired can use functional cast notation ([expr.type.conv]):template<> X Q::x; // declarationtemplate<> X Q::x (); // error: declares a functiontemplate<> X Q::x = X(); // definition
â end note]
A member or a member template of a class template may be explicitly specialized for a given implicit instantiation of the class template, even if the member or member template is defined in the class template definition.
An explicit specialization of a member or member template is specified using the syntax for explicit specialization.
[Example 7: template struct A {void f(T); template void g1(T, X1); template void g2(T, X2); void h(T) { }};
// specializationtemplate<> void A::f(int);
// out of class member template definitiontemplate template void A::g1(T, X1) { }// member template specializationtemplate<> template void A::g1(int, X1);
// member template specializationtemplate<> template<>void A::g1(int, char); // X1 deduced as chartemplate<> template<>void A::g2(int, char); // X2 specified as char// member specialization even if defined in class definitiontemplate<> void A::h(int) { } â end example]
A member or a member template may be nested within many enclosing class templates.
In an explicit specialization for such a member, the member declaration shall be preceded by atemplate<> for each enclosing class template that is explicitly specialized.
[Example 8: template class A {template class B {void mf(); };};template<> template<> class A::B;template<> template<> void A::B::mf(); â end example]
In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well.
In such an explicit specialization declaration, the keywordtemplate followed by atemplate-parameter-list shall be provided instead of thetemplate<> preceding the explicit specialization declaration of the member.
The types of thetemplate-parameters in thetemplate-parameter-list shall be the same as those specified in the primary template definition.
[Example 9: template class A {template class B {template void mf1(T3); void mf2(); };};template <> template class A::B {template void mf1(T); };template <> template <> templatevoid A::B::mf1(T t) { }template template <>void A::B::mf2() { } // error: B is specialized but// its enclosing class template A is not â end example]
A specialization of a member function template, member class template, or static data member template of a non-specialized class template is itself a template.
An explicit specialization declaration shall not be a friend declaration.
Default function arguments shall not be specified in a declaration or a definition for one of the following explicit specializations:
the explicit specialization of a function template;
the explicit specialization of a member function template;
the explicit specialization of a member function of a class template where the class template specialization to which the member function specialization belongs is implicitly instantiated. [Note 5: Default function arguments can be specified in the declaration or definition of a member function of a class template specialization that is explicitly specialized. â end note]