Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to resolve function call in method overloading?

Tags:

c++

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

like image 330
Bharat Avatar asked Jun 17 '11 04:06

Bharat


2 Answers

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.

like image 148
Alok Save Avatar answered Oct 05 '22 13:10

Alok Save


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!)

like image 44
Richard Brightwell Avatar answered Oct 05 '22 13:10

Richard Brightwell