I have my function overloaded in code below:
void function(char x, double y) {
cout << "char, double" << endl;
}
void function(int x, int y) {
cout << "int, int" << endl;
}
int main() {
function('a', 'b');
return 0;
}
When i try to compile it says me: "[Warning] ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second"
How does compiler make implicit conversions here so that it's ambiguous which candidate is right?
In Function overloading, sometimes a situation can occur when the compiler is unable to choose between two correctly overloaded functions. This situation is said to be ambiguous. Ambiguous statements are error-generating statements and the programs containing ambiguity will not compile.
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.
Overloaded functions differentiate between argument types that take different initializers. Therefore, an argument of a given type and a reference to that type are considered the same for the purposes of overloading. They're considered the same because they take the same initializers.
The main advantage of function overloading is that it improves code readability and allows code reusability. The use of function overloading is to save memory space, consistency, and readability. It speeds up the execution of the program.
The literal constants 'a'
and 'b'
have type char
, so there is no exact match. The ambiguity occurs because the first parameter matched the first function, but the preferred conversion of the second is to int
, matching the second function.
GCC is very explicit about this, issuing the following diagnostic:
warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second
GCC seems to be saying, I could resolve this for you, but ISO C++ won't allow it. Type agreement is however important to good code quality and avoidance of errors.
You can coerce the selection by casting:
function( static_cast<int>('a'), static_cast<int>('b') );
or by supplying a function( char, char )
overload.
There are implicit
conversions between double
, int
, and char
in C++, so you should use static_cast<int>
to convert data from char
to int
for example.
function( static_cast<int>(c), static_cast<int>(d) );
this will call function( int, int );
In your particular case, you use 'a'
and 'b'
character literals, which, as I mentioned above, have implicit conversions
to int
and double
, because a char
variable represents the ASCII value of the character entered in the assignment operator
. Hence, we can initialize an unsigned char
variable, or a char
in the ways below:
unsigned char a = 97; // the ASCII decimal code for "a"
unsigned char b = 'a'; // translates into 97
Since an unsigned char
or char
variables is an 8 bits variable, and int
is a 32 bit value, they have implicit conversions.
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