#include <iostream>
void foo(const char* str) // Function 1
{
std::cout << "F1 = " << str << std::endl;
}
void foo(short n) // Function 2
{
std::cout << "F2 = " << n << std::endl;
}
int main()
{
foo("randomString"); // Function 1 is called
foo(1); // Function 2 is called
foo(0);
// In this case 0 can be implictly treated as a "nullptr" or an integer
// so with 1 implicit conversion sequence compiler can deduce either of the Function 1 or 2
// Hence it is an ill-formed function call and a compilation error.
return 0;
}
Compilation Error:
ill-formed-function-call.cpp:16:5: error: call to 'foo' is ambiguous
foo(0);
^~~
ill-formed-function-call.cpp:3:6: note: candidate function
void foo(const char* str) // Function 1
^
ill-formed-function-call.cpp:7:6: note: candidate function
void foo(short n) // Function 2
^
1 error generated.
Note: nullptr constant is an integer literal with value 0.