[temp.spec.partial.match]

# 13 Templates [[temp]](./#temp)

## 13.7 Template declarations [[temp.decls]](temp.decls#temp.spec.partial.match)

### 13.7.6 Partial specialization [[temp.spec.partial]](temp.spec.partial#match)

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

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L3810)

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[.](#1.sentence-1)

This is done by matching the template arguments of the template
specialization with the template argument lists of the partial
specializations[.](#1.sentence-2)

- [(1.1)](#1.1)

  If exactly one matching partial specialization is found, the instantiation is
generated from that partial specialization[.](#1.1.sentence-1)

- [(1.2)](#1.2)

  If more than one matching partial specialization is found,
the partial order rules ([[temp.spec.partial.order]](temp.spec.partial.order "13.7.6.3 Partial ordering of partial specializations")) are used to determine
whether one of the partial specializations is more specialized than the
others[.](#1.2.sentence-1)
  If such a partial specialization exists,
the instantiation is generated from that partial specialization;
otherwise, the use of the template is ambiguous and the program is ill-formed[.](#1.2.sentence-2)

- [(1.3)](#1.3)

  If no matches are found, the instantiation is generated from the
primary template[.](#1.3.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L3836)

A partial specialization matches a given actual template argument
list if the template arguments of the partial specialization can be[deduced](temp.deduct "13.10.3 Template argument deduction [temp.deduct]") from the actual template argument list,
and the deduced template arguments satisfy the [associated constraints](temp.constr.decl#def:associated_constraints "13.5.3 Constrained declarations [temp.constr.decl]") of the partial specialization, if any[.](#2.sentence-1)

[*Example [1](#example-1)*: template<class T1, class T2, int I> class A { }; // #1template<class T, int I> class A<T, T*, I> { }; // #2template<class T1, class T2, int I> class A<T1*, T2, I> { }; // #3template<class T> class A<int, T*, 5> { }; // #4template<class T1, class T2, int I> class A<T1, T2*, I> { }; // #5 A<int, int, 1> a1; // uses #1 A<int, int*, 1> a2; // uses #2, T is int, I is 1 A<int, char*, 5> a3; // uses #4, T is char A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1 A<int*, int*, 2> a5; // ambiguous: matches #3 and #5 â *end example*]

[*Example [2](#example-2)*: template<typename T> concept C = requires (T t) { t.f(); };

template<typename T> struct S { }; // #1template<C T> struct S<T> { }; // #2struct Arg { void f(); };

S<int> s1; // uses #1; the constraints of #2 are not satisfied S<Arg> s2; // uses #2; both constraints are satisfied but #2 is more specialized â *end example*]

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L3871)

If the template arguments of a partial specialization cannot be deduced
because of the structure of its [*template-parameter-list*](temp.pre#nt:template-parameter-list "13.1 Preamble [temp.pre]") and the [*template-id*](temp.names#nt:template-id "13.3 Names of template specializations [temp.names]"),
the program is ill-formed[.](#3.sentence-1)

[*Example [3](#example-3)*: template <int I, int J> struct A {};template <int I> struct A<I+5, I*2> {}; // errortemplate <int I> struct A<I, I> {}; // OKtemplate <int I, int J, int K> struct B {};template <int I> struct B<I, I*2, 2> {}; // OK â *end example*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L3888)

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[.](#4.sentence-1)

The template arguments of a partial specialization are deduced from the arguments
of the primary template[.](#4.sentence-2)
