Description
The template parameter types for a function template are normally determined by a process known as template argument deduction. For example, with this code:
template <class T> void f(T); void g() { f(100); }the type of T can be deduced to be int.
But there are several cases where such deduction is insufficient, and for these cases C++ supports the explicit specification of template parameter types. One example is where the template parameters are simply not used in the function signature:
template <class T> void f();another where you'd like to specify a return type that is independent of the argument types to the function:
template <class T, class U> T findmax(U, U);and a third case where you have a template:
template <class T> T findmin(T, T); void g() { double m = findmin(10, 12.34); }that involves template argument deduction along with one of promotions, standard conversions, or user-defined conversions. None of these are applied when template argument deduction is done, and thus a common type for the arguments cannot be found (because, for example, int is not converted to double in this case).
In all these cases, the solution is to explicitly specify the template parameter types. For example:
double m = findmin<double>(10, 12.34);specifies that T is a double, and resolves the problem with unifying the types of the arguments.
Concept
The sample program defines three templates. The first doesn't use the template parameter in the function declaration. The second is called with arguments of two different types (int and double), and the third has a return type that is unrelated to the argument types to the function. In all of these cases, explicit qualification of template arguments resolves the problem.
Supported
Supported
Supported