The problem is that 5.5
is of type double
. Since print
is overloaded, the compiler will look for the best match to find out which overload to call, a process called overload resolution. This is a quite complex set of rules, but here is what happens in your simple case:
First the compiler will check for an exact match, i.e. some void print(double);
or the like.
Since this does not exist, it considers conversions. A double
can be implicitly converted to both int
and float
, and those conversions are deemed equally good.
Thus, the compiler cannot decide which function overload it should call and complains that the call is ambiguous.
As already mentioned by others, you can fix this by
either getting the input type exactly right: print(5.5f);
or adding an overload that is an unambiguously better match for a double
argument, like e.g.
void print (double);
void print (const double&);
template <class T>
void print (T);