I have the following:
void func(const char *p) { std::cout << p << "\n"; }
void func(std::nullptr_t p) { std::cout << "<null>\n"; }
int main()
{
func("test");
char *p=nullptr;
func(p);
func(nullptr);
return 0;
}
func("test")
is always called. With func(p)
commented, func(nullptr)
is called, but when func(p)
is not commented neither of them are called. Why not? Why does func(p)
with p==nullptr not call func(nullptr), but to calls func(const char*) instead?
[edit]
Based on responses my conclusion is that func(...) is called based on the type of the parameter, and p
is of type char*
, setting p's value to nullptr does not change the type and will not change that func(char*) is called - as the accepted answer also explains.