问题描述
为了了解更多使用指令和函数重载,我尝试了这个程序:
namespace ns{
void f(int){cout << "int\n";}
void f(double){cout << "double\n";}
void f(std::string){cout << "string\n";}
struct Foo{};
}
void f(ns::Foo const&){
cout << "ns::Foo\n";
}
namespace bin{
void f(int*){
std::cout << "bin::f(int*)\n";
}
}
int main(){
using namespace ns;
//using namespace bin;
f(7); // int
f(7.5); // double
f(ns::Foo{}); // ns::Foo
try{
f(nullptr);
}
catch(std::exception const& e){
std::cout << e.what() << std::endl;
}
}
当我运行程序时,它工作正常,除了最后一次调用 f(nullptr)
导致运行时错误:
int
double
ns::Foo
basic_string::_M_construct null not valid
如果我取消注释命名空间 using
的 bin
指令,则代码可以正常工作。
using namespace bin;
输出:
int
double
ns::Foo
bin::f(int*)
解决方法
当 using namespace bin;
被注释掉时,只有 1 个可用的 f()
版本可以将 nullptr
作为输入。 nullptr
不能隐式转换为 int
或 double
,因此排除了 ns::f(int)
和 ns::f(double)
。但是 std::string
可以从 const char*
构造,而 nullptr
可以隐式转换为 const char*
,所以编译器可以构造一个临时的 std::string
对象来传递给 { {1}}。但是,从空 ns::f(std::string)
构造 std::string
是未定义的行为,因此会出现运行时错误(不能保证,顺便说一句,因为行为是未定义的,所以任何事情都可能发生。
当 const char*
没有被注释掉时,有 2 个可用的 using namespace bin;
版本可以将 f()
作为输入。 nullptr
比 bin::f(int*)
更好匹配,因为 ns::f(std::string)
可以隐式转换为 nullptr
,所以不需要构造临时对象,因此编译器选择调用 { {1}} 而不是 int*
。