Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading resolution

Tags:

c++

As far as I know, when coming to choose between two candidate functions the compiler will prefer the one that its weakest match is stronger. For example if I have:

void boo(int i, char c);  
void boo(double d, int i);

for the following code:

float f = 1.0;  
char c = 'c';
boo(f,c);

the second boo should be prefered because its weakest match is promotion while the first one's is standard type conversion.

But when I try to compile it (using gcc), I get:

error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second.

Any ideas?

like image 665
user2668673 Avatar asked Aug 09 '13 17:08

user2668673


1 Answers

Your understanding of overload resolution is wrong. The general rule (when there are more than one argument) is to choose a function for which at least one argument is better (it doesn't matter how much better), and none of the others are worse. In other words, the compiler processes each argument separately, creating a set of "best matches" for it. After this, it takes the union of these sets: if the intersection contains exactly one function, you've won. Otherwise, it's ambiguous.

like image 99
James Kanze Avatar answered Oct 17 '22 06:10

James Kanze