函数查找和命名空间
function lookup and namespaces
如果在调用函数的范围内找不到函数,将在其参数的命名空间中查找。我有几个问题。
如果在不同的命名空间中有多个参数,首先查找哪个命名空间?是第一个参数的命名空间吗?
f(A::T t, B:U u); // Is namespace A looked up first?
模板更复杂类,喜欢
f(A::T<B::U> t); // Namespace A or B is looked up first?
实际上,ADL 的命名空间之间没有顺序。搜索所有相关的名称空间,由此找到的所有函数形成重载解析的候选集。
另请注意,与您在问题中所说的不同,即使在调用范围内通过非限定查找找到函数 时,也会执行 ADL。然后使用非限定查找和 ADL 的联合来查找最佳重载。
ADL 只有在调用作用域的非限定查找发现 class 成员、非函数或块作用域非 using
声明时才会被抑制。
相关规则在C++14 3.4.2 [basic.lookup.argdep]中。引用 N4140,大胆强调我的:
3 Let X be the lookup set produced by unqualified lookup (3.4.1) and let Y be the lookup set produced by
argument dependent lookup (defined as follows). If X contains
- a declaration of a class member, or
- a block-scope function declaration that is not a using-declaration, or
- a declaration that is neither a function or a function template
then Y is empty. Otherwise Y is the set of declarations found in the namespaces associated with the
argument types as described below. The set of declarations found by the lookup of the name is the union of
X and Y.
如果在调用函数的范围内找不到函数,将在其参数的命名空间中查找。我有几个问题。
如果在不同的命名空间中有多个参数,首先查找哪个命名空间?是第一个参数的命名空间吗?
f(A::T t, B:U u); // Is namespace A looked up first?
模板更复杂类,喜欢
f(A::T<B::U> t); // Namespace A or B is looked up first?
实际上,ADL 的命名空间之间没有顺序。搜索所有相关的名称空间,由此找到的所有函数形成重载解析的候选集。
另请注意,与您在问题中所说的不同,即使在调用范围内通过非限定查找找到函数 时,也会执行 ADL。然后使用非限定查找和 ADL 的联合来查找最佳重载。
ADL 只有在调用作用域的非限定查找发现 class 成员、非函数或块作用域非 using
声明时才会被抑制。
相关规则在C++14 3.4.2 [basic.lookup.argdep]中。引用 N4140,大胆强调我的:
3 Let X be the lookup set produced by unqualified lookup (3.4.1) and let Y be the lookup set produced by argument dependent lookup (defined as follows). If X contains
- a declaration of a class member, or
- a block-scope function declaration that is not a using-declaration, or
- a declaration that is neither a function or a function template
then Y is empty. Otherwise Y is the set of declarations found in the namespaces associated with the argument types as described below. The set of declarations found by the lookup of the name is the union of X and Y.