[temp.expl.spec] (original) (raw)
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:
[Example 1: template<class T> class stream;template<> class stream<char> { }; template<class T> class Array { };template<class T> void sort(Array<T>& v) { } template<> void sort<int>(Array<int>&); template<> void sort(Array<char*>&);
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<int> and #3 will be used for arguments of typeArray<char*>; otherArraytypes will be sorted by functions generated from the function template.
— _end example_]
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<int> { }; template<class T> class X;template<> class X<char*> { }; — _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<class T> struct A { struct B { };template<class U> struct C { };};template<> struct A<int> { void f(int);};void h() { A<int> a; a.f(16); } void A<int>::f(int) { } template<> struct A<char>::B { void f();};void A<char>::B::f() { } template<> template<class U> struct A<char>::C { void f();};template<> template<class U> void A<char>::C<U>::f() { } template<> struct A<short>::B { void f();};template<> void A<short>::B::f() { } template<> template<class U> struct A<short>::C { void f();};template<class U> void A<short>::C<U>::f() { } — _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 T> class Array { };template<class T> void sort(Array<T>& v) { } void f(Array<String>& v) { sort(v); } template<> void sort<String>(Array<String>& v); template<> void sort<>(Array<char*>& v); template<class T> struct A { enum E : T;enum class S : T;};template<> enum A<int>::E : int { eint }; template<> enum class A<int>::S : int { sint }; template<class T> enum A<T>::E : T { eT };template<class T> enum class A<T>::S : T { sT };template<> enum A<char>::E : char { echar }; template<> enum class A<char>::S : char { schar }; — _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.
Asimple-template-idthat names a class template explicit specialization that has been declared but not defined can be used exactly like the names of other incompletely-defined classes ([basic.types]).
[Example 5: template<class T> class X; template<> class X<int>; X<int>* p; X<int> x; — _end example_]
[Note 2:
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-specifier_s_appearing in the declaration of a template have no effect on an explicit specialization of that template.
[Example 6: template<class T> void f(T) { } template<class T> inline T g(T) { } template<> inline void f<>(int) { } template<> int g<>(int) { } template<typename> [[noreturn]] void h([[maybe_unused]] int i);template<> void h<int>(int i) { } — _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 3:
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<int>::x; template<> X Q<int>::x (); template<> X Q<int>::x = X();
— _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<class T> struct A { void f(T);template<class X1> void g1(T, X1);template<class X2> void g2(T, X2);void h(T) { } };template<> void A<int>::f(int);template<class T> template<class X1> void A<T>::g1(T, X1) { } template<> template<class X1> void A<int>::g1(int, X1);template<> template<> void A<int>::g1(int, char); template<> template<> void A<int>::g2<char>(int, char); template<> void A<int>::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 T1> class A { template<class T2> class B { void mf();};};template<> template<> class A<int>::B<double>;template<> template<> void A<char>::B<char>::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 keywordtemplatefollowed by atemplate-parameter-listshall be provided instead of thetemplate<>preceding the explicit specialization declaration of the member.
[Example 9: template <class T1> class A { template<class T2> class B { template<class T3> void mf1(T3);void mf2();};};template <> template <class X> class A<int>::B { template <class T> void mf1(T);};template <> template <> template<class T> void A<int>::B<double>::mf1(T t) { } template <class Y> template <> void A<Y>::B<double>::mf2() { } — _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 4:
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_]