gd1*_*gd1 4 c++ namespaces name-lookup argument-dependent-lookup
#include <iostream>
#include <string>
class X {};
namespace N
{
std::string to_string(X)
{
return "foo";
}
void foo()
{
//using std::to_string; // will break the build if uncommented...
//using N::to_string; // ...unless this is uncommented as well
std::cout << to_string(X()) << std::endl;
}
}
int main()
{
N::foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
要么我偶然发现了许多我不掌握的C++ arcana中的一个,或者我在这里遗漏了一些明显的东西.
如何using std::to_string在非限定查找期间将可用的名称集合减少到只能通过ADL访问的名称?虽然我想这个问题可能是to_string(X)在不同的命名空间比声明X,我不禁注意到,如果没有using std::to_string,N::to_string(X)仅仅是可供N::foo()利用"正常"的,直观的查找规则,我习惯了.
这不是特定于using声明,而是遵循正常的范围规则.当您在函数中引入名称时,它会从外部作用域中隐藏同名的内容.
你也可以拥有一个int to_string;局部变量foo.这也会隐藏起来N::to_string.