[temp.spec.partial] (original) (raw)

13.7.6.1 General [temp.spec.partial.general]

A partial specialization of a template provides an alternative definition of the template that is used instead of the primary definition when the arguments in a specialization match those given in the partial specialization ([temp.spec.partial.match]).

A declaration of the primary template shall precede any partial specialization of that template.

A partial specialization shall be reachable from any use of a template specialization that would make use of the partial specialization as the result of an implicit or explicit instantiation; no diagnostic is required.

Two partial specialization declarations declare the same entity if they are partial specializations of the same template and have equivalenttemplate-heads and template argument lists ([temp.over.link]).

Each partial specialization is a distinct template.

[Example 1: template<class T1, class T2, int I> class A { };template<class T, int I> class A<T, T*, I> { };template<class T1, class T2, int I> class A<T1*, T2, I> { };template<class T> class A<int, T*, 5> { };template<class T1, class T2, int I> class A<T1, T2*, I> { };

The first declaration declares the primary (unspecialized) class template.

The second and subsequent declarations declare partial specializations of the primary template.

— _end example_]

A partial specialization may be constrained ([temp.constr]).

[Example 2: template<typename T> concept C = true;template<typename T> struct X { };template<typename T> struct X<T*> { }; template<C T> struct X<T> { };

Both partial specializations are more specialized than the primary template.

#1 is more specialized because the deduction of its template arguments from the template argument list of the class template specialization succeeds, while the reverse does not.

#2 is more specialized because the template arguments are equivalent, but the partial specialization is more constrained ([temp.constr.order]).

— _end example_]

The template argument list of a partial specialization is the template-argument-list following the name of the template.

A partial specialization may be declared in any scope in which the corresponding primary template may be defined ([dcl.meaning], [class.mem], [temp.mem]).

[Example 3: template<class T> struct A { struct C { template<class T2> struct B { };template<class T2> struct B<T2**> { }; };};template<class T> template<class T2> struct A<T>::C::B<T2*> { }; A<short>::C::B<int*> absip; — _end example_]

Partial specialization declarations do not introduce a name.

Instead, when the primary template name is used, any reachable partial specializations of the primary template are also considered.

[Note 1:

One consequence is that a using-declarationwhich refers to a class template does not restrict the set of partial specializations that are found through the using-declaration.

— _end note_]

[Example 4: namespace N { template<class T1, class T2> class A { }; } using N::A; namespace N { template<class T> class A<T, T*> { }; }A<int,int*> a; — _end example_]

A constant template argument is non-specialized if it is the name of a constant template parameter.

All other constant template arguments are specialized.

Within the argument list of a partial specialization, the following restrictions apply:

The usual access checking rules do not apply to non-dependent names used to specify template arguments of the simple-template-idof the partial specialization.

[Note 2:

The template arguments can be private types or objects that would normally not be accessible.

Dependent names cannot be checked when declaring the partial specialization, but will be checked when substituting into the partial specialization.

— _end note_]

13.7.6.2 Matching of partial specializations [temp.spec.partial.match]

When a template is used in a context that requires an instantiation of the template, it is necessary to determine whether the instantiation is to be generated using the primary template or one of the partial specializations.

This is done by matching the template arguments of the template specialization with the template argument lists of the partial specializations.

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can bededuced from the actual template argument list, and the deduced template arguments satisfy the associated constraintsof the partial specialization, if any.

[Example 1: template<class T1, class T2, int I> class A { }; template<class T, int I> class A<T, T*, I> { }; template<class T1, class T2, int I> class A<T1*, T2, I> { }; template<class T> class A<int, T*, 5> { }; template<class T1, class T2, int I> class A<T1, T2*, I> { }; A<int, int, 1> a1; A<int, int*, 1> a2; A<int, char*, 5> a3; A<int, char*, 1> a4; A<int*, int*, 2> a5; — _end example_]

[Example 2: template<typename T> concept C = requires (T t) { t.f(); };template<typename T> struct S { }; template<C T> struct S<T> { }; struct Arg { void f(); }; S<int> s1; S<Arg> s2; — _end example_]

If the template arguments of a partial specialization cannot be deduced because of the structure of its template-parameter-listand the template-id, the program is ill-formed.

[Example 3: template <int I, int J> struct A {};template <int I> struct A<I+5, I*2> {}; template <int I> struct A<I, I> {}; template <int I, int J, int K> struct B {};template <int I> struct B<I, I*2, 2> {}; — _end example_]

In a name that refers to a specialization of a class or variable template (e.g., A<int, int, 1>), the argument list shall match the template parameter list of the primary template.

The template arguments of a partial specialization are deduced from the arguments of the primary template.

13.7.6.3 Partial ordering of partial specializations [temp.spec.partial.order]

For two partial specializations, the first is more specialized than the second if, given the following rewrite to two function templates, the first function template is more specialized than the second according to the ordering rules for function templates:

[Example 1: template<int I, int J, class T> class X { };template<int I, int J> class X<I, J, int> { }; template<int I> class X<I, I, int> { }; template<int I0, int J0> void f(X<I0, J0, int>); template<int I0> void f(X<I0, I0, int>); template <auto v> class Y { };template <auto* p> class Y<p> { }; template <auto** pp> class Y<pp> { }; template <auto* p0> void g(Y<p0>); template <auto** pp0> void g(Y<pp0>);

According to the ordering rules for function templates, the function template_B_is more specialized than the function template_A_and the function template_D_is more specialized than the function template_C_.

Therefore, the partial specialization #2 is more specialized than the partial specialization #1 and the partial specialization #4 is more specialized than the partial specialization #3.

— _end example_]

[Example 2: template<typename T> concept C = requires (T t) { t.f(); };template<typename T> concept D = C<T> && requires (T t) { t.f(); };template<typename T> class S { };template<C T> class S<T> { }; template<D T> class S<T> { }; template<C T> void f(S<T>); template<D T> void f(S<T>);

The partial specialization #2 is more specialized than #1 because B is more specialized than A.

— _end example_]

13.7.6.4 Members of class template partial specializations [temp.spec.partial.member]

The members of the class template partial specialization are unrelated to the members of the primary template.

Class template partial specialization members that are used in a way that requires a definition shall be defined; the definitions of members of the primary template are never used as definitions for members of a class template partial specialization.

An explicit specialization of a member of a class template partial specialization is declared in the same way as an explicit specialization of a member of the primary template.

[Example 1: template<class T, int I> struct A { void f();};template<class T, int I> void A<T,I>::f() { } template<class T> struct A<T,2> { void f();void g();void h();};template<class T> void A<T,2>::g() { } template<> void A<char,2>::h() { } int main() { A<char,0> a0; A<char,2> a2; a0.f(); a2.g(); a2.h(); a2.f(); } — _end example_]

If a member template of a class template is partially specialized, the member template partial specializations are member templates of the enclosing class template; if the enclosing class template is instantiated ([temp.inst], [temp.explicit]), a declaration for every member template partial specialization is also instantiated as part of creating the members of the class template specialization.

If the primary member template is explicitly specialized for a given (implicit) specialization of the enclosing class template, the partial specializations of the member template are ignored for this specialization of the enclosing class template.

If a partial specialization of the member template is explicitly specialized for a given (implicit) specialization of the enclosing class template, the primary member template and its other partial specializations are still considered for this specialization of the enclosing class template.

[Example 2: template<class T> struct A { template<class T2> struct B {}; template<class T2> struct B<T2*> {}; };template<> template<class T2> struct A<short>::B {}; A<char>::B<int*> abcip; A<short>::B<int*> absip; A<char>::B<int> abci; — _end example_]