這似乎有點倒退到我,但它的工作原理:爲什麼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