[temp.over] (original) (raw)
13 Templates [temp]
13.10 Function template specializations [temp.fct.spec]
13.10.3 Overload resolution [temp.over]
When a call to the name of a function or function template is written (explicitly, or implicitly using the operator notation), template argument deduction ([temp.deduct]) and checking of any explicit template arguments ([temp.arg]) 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.
For each function template, if the argument deduction and checking succeeds, thetemplate-arguments(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.
If, for a given function template, 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.
The complete set of candidate functions includes all the synthesized declarations and all of the non-template overloaded functions of the same name.
The synthesized declarations are treated like any other functions in the remainder of overload resolution, except as explicitly noted in [over.match.best].140
[ Example
:
template 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 function
int max(int,int);
to the example above would resolve the third call, by providing a function that could be called formax(a,c)after using the standard conversion ofchartointforc.
— end example
]
[ Example
:
Here is an example involving conversions on a function argument involved intemplate-argumentdeduction:
template struct B { /* ... / }; template struct D : public B { / ... */ }; template void f(B&);
void g(B& bi, D& di) { f(bi); // f(bi) f(di); // f((B&)di) }
— end example
]
[ Example
:
Here is an example involving conversions on a function argument not involved intemplate-parameterdeduction:
template void f(T*,int); // #1 template void f(T,char); // #2
void h(int* pi, int i, char c) { f(pi,i); // #1: f(pi,i) f(pi,c); // #2: f<int*>(pi,c)
f(i,c); // #2: f(i,c); f(i,i); // #2: f(i,char(i)) }
— end example
]
Only the signature of a function template specialization is needed to enter the specialization in a set of candidate functions.
Therefore only the function template declaration is needed to resolve a call for which a template specialization is a candidate.
[ Example
:
template void f(T); // declaration
void g() { f("Annemarie"); // call of f<const char*> }
The call offis well-formed even if the templatefis only declared and not defined at the point of the call.
The program will be ill-formed unless a specialization forf<const char*>, either implicitly or explicitly generated, is present in some translation unit.
— end example
]
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.