[temp.over] (original) (raw)
13 Templates [temp]
13.10 Function template specializations [temp.fct.spec]
13.10.4 Overload resolution [temp.over]
When a call 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 or, for conversion function templates, that can convert to the required type.
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 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.
The complete set of candidate functions includes all the synthesized declarations and all of the non-template functions found by name lookup.
The synthesized declarations are treated like any other functions in the remainder of overload resolution, except as explicitly noted in [over.match.best].121
[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); char m2 = max(c,d); int m3 = max(a,c); }
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 ofchartointforc.
— _end example_]
[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(di); }
— _end example_]
[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); template<class T> void f(T,char); void h(int* pi, int i, char c) { f(pi,i); f(pi,c); f(i,c); f(i,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 4: template<class T> void f(T); void g() { f("Annemarie"); }
The call tofis 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*>is explicitly instantiated in some translation unit ([temp.pre]).
— _end example_]