针对函数的依赖于自变量的名称 (Koenig) 查找

编译器可以使用依赖于自变量的名称查找来查找非限定函数调用的定义。 依赖于自变量的名称查找也称为 Koenig 查找。 在命名空间、类、结构、联合或模板的层次结构中定义函数调用中每个自变量的类型。 当指定未限定的后缀函数调用时,编译器将在与每个自变量类型关联的层次结构中搜索函数定义。

示例

在此示例中,编译器注意到函数 f() 采用了自变量 x。 自变量 x 是命名空间 A::X 中定义的类型 A。 编译器搜索命名空间 A 并查找采用类型 f() 的参数的函数 A::X 的定义。

// argument_dependent_name_koenig_lookup_on_functions.cpp
namespace A
{
   struct X
   {
   };
   void f(const X&)
   {
   }
}
int main()
{
// The compiler finds A::f() in namespace A, which is where
// the type of argument x is defined. The type of x is A::X.
   A::X x;
   f(x);
}