ambiguous call to overloaded function

S

subramanian100in

Consider the following program:

#include <iostream>

using namespace std;

void print(char c)
{
cout << "from print(char c) : " << c << endl;
return;
}

void print(long c)
{
cout << "from print(long c) : " << c << endl;
return;
}

int main()
{
int i = 100;
print(i);

return 0;
}

When I compile the program I am getting the following compilation
error:

'print' : ambiguous call to overloaded function
could be 'void print(long)'
or 'void print(char)'
while trying to match the argument list '(int)'

int to char requires standard conversion.
But isn't integer promoted to long similar to the way short is
promoted to int ? If int is promoted to long, the ambiguity will not
arise. This is what I expected. But it does not happen. Kindly explain
the reason.

Thanks
V.Subramanian
 
K

Kai-Uwe Bux

Consider the following program:

#include <iostream>

using namespace std;

void print(char c)
{
cout << "from print(char c) : " << c << endl;
return;
}

void print(long c)
{
cout << "from print(long c) : " << c << endl;
return;
}

int main()
{
int i = 100;
print(i);

return 0;
}

When I compile the program I am getting the following compilation
error:

'print' : ambiguous call to overloaded function
could be 'void print(long)'
or 'void print(char)'
while trying to match the argument list '(int)'

int to char requires standard conversion.
But isn't integer promoted to long similar to the way short is
promoted to int ? If int is promoted to long, the ambiguity will not
arise. This is what I expected. But it does not happen. Kindly explain
the reason.

There is no promotion from int to long, it's a conversion. See clause [4.5]
for the list of integral promotions and see clause [4.7] for the definition
of integral conversions.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top