When you are working with function overload there is a lot of work that the compiler does behind the scenes to perform function overloading resolution. The details are to great to list here, however I can provide some links that may be of use and try to describe some of the more important parts.
There are 3 possible results:
- A match is found. The call is resolved to specific overload
- No match found. The arguments can not be matched to any overload
- An ambiguous match is found. Arguments matched more than one overload
The basic order of what the compiler will do is this:
- Find exact match based on parameter list
- Tries to find a match through promotion
- Tries to find a match through standard conversion
- Tries to find a match through user-defined conversion
So how is it determined if a call is ambiguous?
Since every overload has to have unique parameters and due to the fact that all standard conversions and all user-defined conversions are considered equal; if a function call matches more than one valid declaration-definition candidate through standard or user-defined conversion then the result will be ambiguous.
From Your Example:
void print( unsigned int value );
void print( float value );
print( 'a' );
print( 0 );
print( 3.14159 );
In the case of print( 'a' );
C++ can not find an exact match. It will first try to promote 'a'
to an int
, but there is no print(int)
function that is declared-defined. It will then try to use standard conversion, it can convert 'a'
to both an unsigned int
and a floating point
value. And since all standard conversions are considered equal, this results in ambiguity.
To resolve this ambiguity you can either simply declare-define
the needed versions of print( type );
or you can explicitly cast the type to one of the provided overload parameter types.