Argument-denpendent Lookup(ADL), 是为了在函数调用表达式中过程中,查找unqualified 【非限定】 function name 而定义的规则, 包括:implicit function 到重载操作。 除了通常unqualified [非限定]的名字查找 name lookup所考虑的scopes[作用域] 和namespaces命名空间之外, 还可以在其他的参数自己的 名称空间 来查找这些函数名字。
These function names are looked up in the namespaces of their arguments in addition to the scopes and namespaces considered by the usual unqualified name lookup.
1. Name lokup 名字查找规则
name lookup 是编译器将一个名字关联到该名字的声明的过程,在程序中如果遇到一个name的时候。例如:
For example, to compile std::cout << std::endl;, the compiler performs:
- unqualified name lookup (非限定名字查找)for the name
std
, which finds the declaration of namespace std in the header<iostream>
- qualified name lookup (限定名字查找) for the name
cout
, which finds a variable declaration in the namespacestd
- qualified name lookup for the name
endl
, which finds a function template declaration in the namespacestd
- both argument-dependent lookup [参数依赖的查找] for the name
operator <<
which finds multiple function template declarations in the namespace std and qualified name lookup for the namestd::ostream::operator<<
which finds multiple member function declarations in classstd::ostream
For function and function template names, name lookup can associate multiple declarations with the same name, and may obtain additional declarations from argument-dependent lookup. Template argument deduction may also apply, and the set of declarations is passed to overload resolution, which selects the declaration that will be used. Member access rules, if applicable, are considered only after name lookup and overload resolution.
对于函数和函数模板名字, name lookup 可以与同样的名字关联多个声明, 且可以从argument-dependent lookup 获得额外的声明。 模板参数推导 Template argument deduction 也会应用, 声明集合将被 重载方法解析,从而选择使用的声明。 在name lookup 和over resolution 之后,则是 成员访问【member access】
For all other names (variables, namespaces, classes, etc), name lookup must produce a single declaration 【唯一声明】in order for the program to compile. Lookup for a name in a scope finds all declarations of that name, with one exception, known as the "struct hack【结构hack】" or "type/non-type hiding【类型或非类型隐藏】": Within the same scope, some occurrences of a name may refer to a declaration of a class/struct/union/enum that is not a typedef, while all other occurrences of the same name either all refer to the same variable, non-static data member (since C++14), or enumerator, or they all refer to possibly overloaded function or function template names. In this case, there is no error, but the type name is hidden from lookup (the code must use elaborated type specifier to access it).
If the name appears immediately to the right of the scope resolution operator 【作用域解析符号】::
or possibly after ::
followed by the disambiguating keyword 【消除歧义的】template
, see 则需要“Qualified Name Lookup 限定符名字查找”,
否则进行 “Unqualified name lookup 非限定符名字查找”,即对于函数,则进行参数依赖的查找规则【argument dependent lookup】。
1. 限定名称查询:
Qualified name lookup
endL;