c++ - Argument-Dependent Lookup 是否在正常范围查找之前进行?

标签 c++ scope namespaces argument-dependent-lookup using-declaration

这是“C++ Primer”第 5 版第 13.3 节中出现的相关代码:

void swap(Foo &lhs, Foo &rhs)
{
    using std::swap;
    swap(lhs.h, rhs.h); // uses the HasPtr version of swap
    // swap other members of type Foo
}

书中提到类特定交换没有被using声明隐藏的现象,并请读者引用§18.2.3:

enter image description here

我阅读了该部分并意识到这可能与参数相关查找 (ADL) 有关。以下为摘录:

enter image description here

但是我在理解上还是有些模糊。我的问题是:ADL 是在正常范围查找之前进行,还是在正常范围查找之后进行?我目前的理解是 ADL 在正常范围查找之前进行,否则它应该是使用的 std::swap 。如果你认为我是对的,我需要确认,或者如果你认为我错了,请指出我犯了什么错误。谢谢。

最佳答案

ADL没去过,不是特别喜欢;除了通过通常的名称查找找到的名称之外,还将考虑通过 ADL 找到的名称。

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.

这意味着 ADL 找到的所有命名和通常的名称查找都将在 overload resolution 中考虑;然后将选择最佳匹配。

In order to compile a function call, the compiler must first perform name lookup, which, for functions, may involve argument-dependent lookup, and for function templates may be followed by template argument deduction. If these steps produce more than one candidate function, then overload resolution is performed to select the function that will actually be called.

关于c++ - Argument-Dependent Lookup 是否在正常范围查找之前进行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46782156/

相关文章:

php - 未找到命名空间类

symfony - 在 Symfony 2.2 中找不到类

c++ - 在 C++ 中,可以重命名/别名非命名空间、非类名吗?

c++ - 这个 C++ 代码会一直按我的预期工作,还是不能保证执行顺序?

c++ - 菜单驱动逻辑错误导致死循环

c++ - 类内和类外的 friend 功能,有什么区别?

python - 什么时候检查非局部变量的存在?

c++ - 内存和范围管理

c++ - Qt 5 构建错误 : extra characters after test expression

C++:如果抛出异常,超出范围的对象是否被销毁?