[temp.deduct.call]14.8.2.1 Deducing template arguments from a function call
1Template argument deduction is done by comparing each function
template parameter type (call it
P
)
with the type of the corresponding argument of the call (call it
A
)
as described below.
If P
is a dependent type,
removing references and cvqualifiers from P
gives
std::initializer_list<P′>
or P′[N]
for some P′
and N
and the
argument is a nonempty initializer list (8.5.4), then deduction is
performed instead for each element of the initializer list, taking
P′ as a function template parameter type and the initializer
element as its argument,
and in the P′[N]
case, if N
is a nontype template parameter,
N
is deduced from the length of the initializer list.
Otherwise, an initializer list argument causes the
parameter to be considered a nondeduced context (14.8.2.5).
[ Example:
template<class T> void f(std::initializer_list<T>);
f({1,2,3}); // T deduced to int
f({1,"asdf"}); // error: T deduced to both int and const char*
template<class T> void g(T);
g({1,2,3}); // error: no argument deduced for T
template<class T, int N> void h(T const(&)[N]);
h({1,2,3}); // T deduced to int, N deduced to 3
template<class T> void j(T const(&)[3]);
j({42}); // T deduced to int, array bound not considered
struct Aggr { int i; int j; };
template<int N> void k(Aggr const(&)[N]);
k({1,2,3}); // error: deduction fails,
// no conversion from int to Aggr
k({{1},{2},{3}}); // OK, N deduced to 3
template<int M, int N> void m(int const(&)[M][N]);
m({{1,2},{3,4}}); // M and N both deduced to 2
template<class T, int N> void n(T const(&)[N], T);
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
— end example ]
For a function parameter pack that occurs at the end of the parameterdeclarationlist,
the type A
of each
remaining argument of the call is compared with the type P
of the declaratorid of the function parameter pack.
Each comparison deduces template arguments for subsequent positions in
the template parameter packs expanded by the function parameter pack.
When a function parameter pack appears in a nondeduced
context (14.8.2.5), the type of the parameter pack is
never deduced.
[ Example:
template<class ... Types> void f(Types& ...);
template<class T1, class ... Types> void g(T1, Types ...);
template<class T1, class ... Types> void g1(Types ..., T1);
void h(int x, float& y) {
const int z = x;
f(x, y, z); // Types is deduced to int, float, const int
g(x, y, z); // T1 is deduced to int;
// Types is deduced to float, int
g1(x, y, z); // error: Types is not deduced
g1<int, int, int>(x, y, z); // OK, no deduction occurs
}
— end example ]
2If
P
is not a reference type:

2.1If
A
is an array type, the pointer type produced by the arraytopointer standard conversion (4.2) is used in place ofA
for type deduction; otherwise, 
2.2If
A
is a function type, the pointer type produced by the functiontopointer standard conversion (4.3) is used in place ofA
for type deduction; otherwise, 
2.3If
A
is a cvqualified type, the top level cvqualifiers ofA
’s type are ignored for type deduction.
3
If
P
is a cvqualified type, the top level cvqualifiers of
P
’s
type are ignored for type deduction.
If
P
is a reference type, the type
referred to by
P
is used for type deduction.
A forwarding reference
is an rvalue reference to a cvunqualified template parameter.
If P
is a forwarding reference and the argument is an
lvalue, the type “lvalue reference to A
” is used in place of A
for type
deduction.
[ Example:
template <class T> int f(T&&);
template <class T> int g(const T&&);
int i;
int n1 = f(i); // calls f<int&>(int&)
int n2 = f(0); // calls f<int>(int&&)
int n3 = g(i); // error: would call g<int>(const int&&), which
// would bind an rvalue reference to an lvalue
— end example ]
4In general, the deduction process attempts to find template argument
values that will make the deduced
A
identical to
A
(after
the type
A
is transformed as described above).
However, there are
three cases that allow a difference:

4.1If the original
P
is a reference type, the deducedA
(i.e., the type referred to by the reference) can be more cvqualified than the transformedA
. 
4.2The transformed
A
can be another pointer or pointer to member type that can be converted to the deducedA
via a qualification conversion (4.4). 
4.3If
P
is a class andP
has the form simpletemplateid, then the transformedA
can be a derived class of the deducedA
. Likewise, ifP
is a pointer to a class of the form simpletemplateid, the transformedA
can be a pointer to a derived class pointed to by the deducedA
.
[ Note: as specified in 14.8.1, implicit conversions will be performed on a function argument to convert it to the type of the corresponding function parameter if the parameter contains no templateparameters that participate in template argument deduction. Such conversions are also allowed, in addition to the ones described in the preceding list. — end note ]
5These alternatives are considered only if type deduction would
otherwise fail.
If they yield more than one possible deduced
A
,
the type deduction fails.
[ Note: If a
templateparameter
is not used in any of the function parameters of a function template,
or is used only in a nondeduced context, its corresponding
templateargument
cannot be deduced from a function call and the
templateargument
must be explicitly specified. — end note ]
6When P
is a function type, pointer to function type, or pointer to member
function type:
 6.1If the argument is an overload set containing one or more function templates, the parameter is treated as a nondeduced context.
 6.2If the argument is an overload set (not containing function templates), trial argument deduction is attempted using each of the members of the set. If deduction succeeds for only one of the overload set members, that member is used as the argument value for the deduction. If deduction succeeds for more than one member of the overload set the parameter is treated as a nondeduced context.
7[ Example:
// Only one function of an overload set matches the call so the function
// parameter is a deduced context.
template <class T> int f(T (*p)(T));
int g(int);
int g(char);
int i = f(g); // calls f(int (*)(int))
— end example ]
8[ Example:
// Ambiguous deduction causes the second function parameter to be a
// nondeduced context.
template <class T> int f(T, T (*p)(T));
int g(int);
char g(char);
int i = f(1, g); // calls f(int, int (*)(int))
— end example ]
9[ Example:
// The overload set contains a template, causing the second function
// parameter to be a nondeduced context.
template <class T> int f(T, T (*p)(T));
char g(char);
template <class T> T g(T);
int i = f(1, g); // calls f(int, int (*)(int))
— end example ]