考虑下面的代码。fun
尽管接受指针的两个重载,传递nullptr
到fun
都不会导致任何编译错误。然而,非常相似的函数bun
无法编译。当我i
使用打印参数的类型时typeid(i).name()
(在修改代码只是为了打印它之后)我得到相同的类型,只是int*
. 在 case 中解决歧义fun
但失败的规则是什么bun
?提前致谢!
#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