Friday, 8 July 2016

What happens when float argument is passed to overloaded function with int and char as parameters(Type Conversion rules)?

 
Guess the output?

#include<iostream>

void PostInArrayNotFound(int a)
{
std::cout<<"Int is called"<<std::endl;
}

void PostInArrayNotFound(char a)
{
std::cout<<"Unsigned int is called"<<std::endl;
}

int main()
{
PostInArrayNotFound(5.22);
            
return 0;
}

Output:

It will give an compilation error.

EXPLANATION


There is no function with name “PostInArrayNotFound” and double as an argument.
I think, now compiler is in big tension, to call which “PostInArrayNotFound” function now?
Because float can be implicitly converted to any one of the integral type. Note carefully integral type can be char, unsigned char, int, unsigned int.

So because of this confusion, compiler will throw error on you like, call to the “PostInArrayNotFound” function is ambiguous.