void add(int,int);
void add(int ,float);
void add(float,int);
unsigned int i = 10;
unsigned float j = 1.0;
add(i,f); // ambiguios call error
if i remove unsigned from program then it is working fine.
int i = 10;
float j = 1.0;
add(i,f); // working
Why using unsigned variable in overloading function causes ambiguios call
There is nothing called as unsigned float
in C++. float
is always signed
As per, C++ Standard table 7 in §7.1.5.2, "signed" by itself is a synonym for "int".
So the compiler should give you a error, that signed
or unsigned
is not applicable for float
.
Check here, even Ideone reports an error.
error: ‘signed’ or ‘unsigned’ invalid for ‘j’
Are you by any chance misinterpreting this error as ambiguos
function call error?
If you drop the unsigned float
, the compiler cannot see any matching function call which has arguments unsigned int
& float
, So it promotes unsigned int
to int
and resolves the call to the function with arguments int
& float
, there is no ambiguity.
Here is the code sample on Ideone.
The call is ambiguous because none of your function signatures match (due to looking for signed values) and if it starts casting then it could match more than one signature, so it doesn't know which you want. Add overloads for unsigned values to avoid confusion. (Not so sure about unsigned float!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With