In short, the overload resolution process looks for a function that’s the best match. If there’s just one, that function is chosen. If more than one are otherwise tied, but only one is a non template function, that non template function is chosen. If more than one candidate are otherwise tied and all are template functions, but one template is more specialized than the rest, that one is chosen. If there are two or more equally good non template functions, or if there are two or more equally good template functions, none of which is more specialized than the rest, the function call is ambiguous and an error. If there are no matching calls, of course, that is also an error.
Making Your Own Choices
In some circumstances, you can lead the compiler to make the choice you want by suitably writing the function call. Consider Listing 8.15, which, by the way, eliminates the template prototype and places the template function definition at the top of the file. As with regular functions, a template function definition can act as its own prototype if it appears before the function is used.
Listing 8.15. choices.cpp
// choices.cpp -- choosing a template
#include
template
T lesser(T a, T b) // #1
{
return a < b ? a : b;
}
int lesser (int a, int b) // #2
{
a = a < 0 ? -a : a;
b = b < 0 ? -b : b;
return a < b ? a : b;
}
int main()
{
using namespace std;
int m = 20;
int n = -30;
double x = 15.5;
double y = 25.9;
cout << lesser(m, n) << endl; // use #2
cout << lesser(x, y) << endl; // use #1 with double
cout << lesser<>(m, n) << endl; // use #1 with int
cout << lesser
return 0;
}
(The final function call converts double to int, and some compilers will issue warnings about that.)
Here is the program output:
20
15.5
-30
15
Listing 8.15 provides a template that returns the lesser of two values and a standard function that returns the smaller absolute value of two values. If a function definition appears before its first use, the definition acts as a prototype, so this example omits the prototypes. Consider the following statement:
cout << lesser(m, n) << endl; // use #2
The function call arguments match both the template function and the non template function, so the non template function is chosen, and it returns the value 20.
Next, the function call in the statement matches the template, with type T taken to be double:
cout << lesser(x, y) << endl; // use #1 with double
Now consider this statement:
cout << lesser<>(m, n) << endl; // use #1 with int
The presence of the angle brackets in lesser<>(m, n) indicates that the compiler should choose a template function rather than a non template function, and the compiler, noting that the actual arguments are type int, instantiates the template using int for T.
Finally, consider this statement:
cout << lesser
Here we have a request for an explicit instantiation using int for T, and that’s the function that gets used. The values of x and y are type cast to type int, and the function returns an int value, which is why the program displays 15 instead of 15.5.
Functions with Multiple Type Arguments
Where matters really get involved is when a function call with multiple arguments is matched to prototypes with multiple type arguments. The compiler must look at matches for all the arguments. If it can find a function that is better than all the other viable functions, it is chosen. For one function to be better than another function, it has to provide at least as good a match for all arguments and a better match for at least one argument.
This book does not intend to challenge the matching process with complex examples. The rules are there so that there is a well-defined result for any possible set of function prototypes and templates.
Template Function Evolution
In the early days of C++, most people didn’t envision how powerful and useful template functions and template classes would prove to be. (Probably they didn’t even expend their envisionary powers on the topic.) But clever and dedicated programmers pushed the limits of template techniques and expanded the ideas of what was possible. Feedback from those who developed familiarity with templates led to changes that were incorporated into the C++98 Standard as well as the addition of the Standard Template Library. Since then, template programmers have continued to explore the possibilities offered by the genre, and occasionally they bump up against limitations. Their feedback has led to some changes in the C++11 Standard. We’ll look at a couple of related problems now and their solutions.
What’s That Type?