[temp.over]

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

## 13.10 Function template specializations [[temp.fct.spec]](temp.fct.spec#temp.over)

### 13.10.4 Overload resolution [temp.over]

[1](#1)

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

When a call of a function or function template
is written (explicitly, or implicitly using the
operator notation), template argument deduction ([[temp.deduct]](temp.deduct "13.10.3 Template argument deduction"))
and checking of any explicit template arguments ([[temp.arg]](temp.arg "13.4 Template arguments")) are performed
for each function template to find the template argument values (if any) that
can be used with that function template to instantiate a function template
specialization that can be invoked with the call arguments or,
for conversion function templates, that can convert to the required type[.](#1.sentence-1)

For each function template:

- [(1.1)](#1.1)

  If the argument deduction and checking succeeds, the[*template-argument*](temp.names#nt:template-argument "13.3 Names of template specializations [temp.names]")*s* (deduced and/or explicit)
are used to synthesize the declaration of
a single function template specialization which is
added to the candidate functions set to be used in overload resolution[.](#1.1.sentence-1)

- [(1.2)](#1.2)

  If the argument deduction fails or
the synthesized function template specialization would be ill-formed,
no such function is added to the set of candidate functions for that template[.](#1.2.sentence-1)

The complete set of candidate functions includes all the synthesized
declarations and all of the non-template functions found by name lookup[.](#1.sentence-3)

The synthesized declarations are
treated like any other functions in
the remainder of overload resolution, except as explicitly noted
in [[over.match.best]](over.match.best "12.2.4 Best viable function")[.](#1.sentence-4)[121](#footnote-121 "The parameters of function template specializations contain no template parameter types. The set of conversions allowed on deduced arguments is limited, because the argument deduction process produces function templates with parameters that either match the call arguments exactly or differ only in ways that can be bridged by the allowed limited conversions. Non-deduced arguments allow the full range of conversions. Note also that [over.match.best] specifies that a non-template function will be given preference over a template specialization if the two functions are otherwise equally good candidates for an overload match.")

[2](#2)

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

[*Example [1](#example-1)*: template<class T> T max(T a, T b) { return a>b?a:b; }void f(int a, int b, char c, char d) {int m1 = max(a,b); // max(int a, int b)char m2 = max(c,d); // max(char a, char b)int m3 = max(a,c); // error: cannot generate max(int,char)}

Adding the non-template functionint max(int,int); to the example above would resolve the third call, by providing a function that
can be called formax(a,c) after using the standard conversion ofchar toint forc[.](#2.sentence-1)

â *end example*]

[3](#3)

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

[*Example [2](#example-2)*:

Here is an example involving conversions on a function argument involved in
template argument deduction:template<class T> struct B { /* ... */ };template<class T> struct D : public B<T> { /* ... */ };template<class T> void f(B<T>&);

void g(B<int>& bi, D<int>& di) { f(bi); // f(bi) f(di); // f((B<int>&)di)}

â *end example*]

[4](#4)

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

[*Example [3](#example-3)*:

Here is an example involving conversions on a function argument not involved in
template argument deduction:template<class T> void f(T*,int); // #1template<class T> void f(T,char); // #2void h(int* pi, int i, char c) { f(pi,i); // #1: f<int>(pi,i) f(pi,c); // #2: f<int*>(pi,c) f(i,c); // #2: f<int>(i,c); f(i,i); // #2: f<int>(i,char(i))}

â *end example*]

[5](#5)

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

Only the signature of a function template specialization is needed to enter the
specialization in a set of candidate functions[.](#5.sentence-1)

Therefore only the function template declaration is needed to resolve a call
for which a template specialization is a candidate[.](#5.sentence-2)

[*Example [4](#example-4)*: template<class T> void f(T); // declarationvoid g() { f("Annemarie"); // calls f<const char*>}

The call tof is well-formed even if the templatef is only declared and not defined at the point of the call[.](#5.sentence-3)

The program will be ill-formed unless a specialization forf<const char*> is explicitly instantiated in some translation unit ([[temp.pre]](temp.pre "13.1 Preamble"))[.](#5.sentence-4)

â *end example*]

[121)](#footnote-121)[121)](#footnoteref-121)

The parameters of function template
specializations contain no
template parameter types[.](#footnote-121.sentence-1)

The set of conversions allowed on deduced arguments is limited, because the
argument deduction process produces function templates with parameters that
either match the call arguments exactly or differ only in ways that can be
bridged by the allowed limited conversions[.](#footnote-121.sentence-2)

Non-deduced arguments allow the full range of conversions[.](#footnote-121.sentence-3)

Note also that [[over.match.best]](over.match.best "12.2.4 Best viable function") specifies that a non-template function will
be given preference over a template specialization if the two functions
are otherwise equally good candidates for an overload match[.](#footnote-121.sentence-4)
