Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded function. Why it's ambiguous here?

Tags:

c++

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?

like image 921
Felix Vein Avatar asked Mar 11 '14 10:03

Felix Vein


People also ask

How overloaded function is ambiguous?

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.

What does it mean when a function is overloaded?

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.

How are overloaded functions differentiated?

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.

Why do we need overloaded functions?

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.


2 Answers

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.

like image 62
Clifford Avatar answered Oct 05 '22 23:10

Clifford


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.

like image 33
Victor Avatar answered Oct 06 '22 00:10

Victor