Overloaded functions are selected on a best-match basis. That is, the set of overloaded functions is scanned to see which function declaration in the current scope best matches the arguments supplied in the function call. If a suitable function is found, that function is called. “Suitable” in this context means one of the following:
A standard conversion to the desired argument type exists.
A user-defined conversion (either conversion operator or constructor) to the desired argument type exists.
The compiler creates a list of candidate functions for each argument. Candidate functions are functions in which the argument in that position exactly matches the type of the supplied argument, or for which a suitable conversion can be found.
A list of “best matching functions” is built for each argument, and the selected function is the intersection of all the lists. If the intersection contains more than one function, the overloading is ambiguous and generates an error. The function that is eventually selected is always a better match than every other function in the group for at least one argument. If this is not the case (if there is no clear winner), the function call generates an error.
Consider the following declarations (the functions are marked Variant 1, Variant 2, and Variant 3, for identification in the following discussion):
Fraction &Add( Fraction &f, long l ); // Variant 1
Fraction &Add( long l, Fraction &f ); // Variant 2
Fraction &Add( Fraction &f, Fraction &f ); // Variant 3
Fraction F1, F2;
Consider the following statement:
F1 = Add( F2, 23 );
The preceding statement builds two lists:
List 1: Candidate Functions That Have First Argument of Type Fraction, List 2: Candidate Functions That Have Second Argument of Type int |
Variant 1 | Variant 1 (int can be converted to long using a standard conversion) |
Variant 3, |
The intersection of these two lists is Variant 1. An example of an ambiguous function call is:
F1 = Add( 3, 6 );
The preceding function call builds the following lists:
List 1: Candidate Functions That Have First Argument of Type int, List 2: Candidate Functions That Have Second Argument of Type int |
Variant 2 (int can be converted to long using a standard conversion) | Variant 1 (int can be converted to long using a standard conversion) |
Note that the intersection between these two lists is empty. Therefore, the compiler generates an error message.
For argument matching, a function with n default arguments is treated as n+1 separate functions, each with a different number of arguments.
The ellipsis (...) acts as a wildcard; it matches any actual argument. This can lead to many ambiguous lists, if you do not design your overloaded function lists with extreme care.
Note:
Ambiguity of overloaded functions cannot be determined until a function call is encountered. At that point, the lists are built for each argument in the function call, and it can be determined whether an unambiguous overload exists. This means that ambiguities can remain in your code until they are evoked by a particular function call.