Consider the code below. Although both overloads of fun
accept pointers, passing nullptr
to fun
does not result in any compilation error. Whereas, the very similar function bun
fails to compile. When I print the the types of the argument i
using typeid(i).name()
(after modifying the code just to get this printed) I get the same type, simply int*
. What is the rule that resolves the ambiguity in fun
case, but fails for bun
? Thanks in advance!
#include <iostream>
struct Foo {
int sth;
};
template< class U>
void fun(decltype(U::sth)* i){
std::cout << "1" << std::endl;
}
template< class U>
void fun(U* i){
std::cout << "2" << std::endl;
}
void bun(decltype(Foo::sth)* i){
std::cout << "3" << std::endl;
}
void bun(Foo* i){
std::cout << "4" << std::endl;
}
int main ( )
{
fun<Foo>(nullptr);
// bun(nullptr); --> call of overloaded 'bun(std::nullptr_t)' is ambiguous
return 0;
}
-----------------------
output : 1