【发布时间】:2016-02-07 10:44:54
【问题描述】:
#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;
}
要么我偶然发现了许多我不掌握的 C++ 奥秘之一,要么我在这里遗漏了一些明显的东西。
using std::to_string 如何明显将在非限定查找期间可用的名称集减少到仅可通过 ADL 访问的名称集?虽然我可以猜到问题可能是 to_string(X) 被声明在与 X 不同的命名空间中,但我不禁注意到,如果没有 using std::to_string,N::to_string(X) 可以通过使用“普通",我习惯的直观查找规则。
【问题讨论】:
标签: c++ namespaces argument-dependent-lookup name-lookup