7
綜觀boost::polygon
的源代碼,我已經看到了以下主題的多種應用:調用函數命名空間中的無資質
#include <iostream>
namespace B {
struct A {
void foo() const { std::cout << "foo" << std::endl; }
};
void bar(const A &a) { a.foo(); }
void baz() { std::cout << "baz" << std::endl; }
}
int main(int argc, char **argv) {
B::A a;
bar(a);
B::baz(); // simply calling baz() does not work
return 0;
}
它是如何bar(a)
可以無需額外的資格叫什麼名字?我本來預計只有B::bar(a)
會編譯。
當函數在名稱空間內沒有參數時,不會發生這種情況。
查找「ADL」(參數依賴查詢)又名「Koenig查找」。 – nwp
關於cppreference的ADL參考:http://en.cppreference.com/w/cpp/language/adl – NathanOliver
謝謝,那很快:) – vukung