这似乎有点倒退到我,但它的工作原理:为什么C++参数范围影响名称空间内的函数查找?
#include <iostream>
namespace nTest
{
struct cTest {};
void fTest(cTest& x)
{
std::cout << "nTest::fTest(cTest&) called" << std::endl;
}
}
int main(void)
{
nTest::cTest x;
fTest(x); //Weird! fTest is resolved since its parameter belongs to nTest.
return 0;
}
通常情况下,你需要NTEST ::才能访问FTEST,但属于NTEST它的参数似乎NTEST增加的可能名单在其中搜索fTest的范围。对我来说,参数范围影响函数查找似乎很奇怪。
这在GCC中编译得很好,但我想知道这种用法是否便携?这个范围界定机制的官方定义是什么?
ADL - 参数依赖查找(http://en.wikipedia.org/wiki/Argument-dependent_name_lookup) – Nim 2011-03-22 14:21:20
也读取接受的答案[here](http://stackoverflow.com/questions/2958648/what-are -the-pitfalls-of-adl) – 2011-03-22 14:23:24