16.2.1. Template Argument Deduction
To determine which functions to instantiate, thecompiler looks at each argument. If the corresponding parameter wasdeclared with a type that is a type parameter, then the compiler infersthe type of the parameter from the type of the argument. In the case of compare, both arguments have the same template type: they were eachdeclared using the type parameter T.
Inthe first call, compare(1, 0), those arguments are type int; in the second,compare, they have type double. The process of determining the types andvalues of the template arguments from the type of the function argumentsis called template argument deduction .
MultipleType Parameter Arguments Must Match Exactly
A template type parameter may be used as the type of more than one functionparameter. In such cases, template type deduction must generate the sametemplate argument type for each corresponding function argument. If thededuced types do not match, then the call is an error:
template <typename T>
int compare(const T& v1, const T& v2)
{
if (v1 < v2) return -1;
if (v2 < v1) return 1;
return 0;
}
int main()
{
short si;
// error: cannot instantiate compare(short, int)
// must be: compare(short, short) or
// compare(int, int)
compare(si, 1024);
return 0;
}
This call is in error because the arguments to comparedon't have the same type.The template argument deduced from the first ar