依赖于参数的查找是否在普通范围查找之前进行? [英] Does Argument-Dependent Lookup go before normal scope lookup?

查看:82
本文介绍了依赖于参数的查找是否在普通范围查找之前进行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有问题的代码,显示在5ed的"C ++ Primer"的第13.3节中:

This is the code in question that appears in §13.3 of "C++ Primer", 5ed:

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
}

这本书提到了使用声明未隐藏特定于类的交换的现象,并向读者介绍了§18.2.3:

The book mentions the phenomenon of the class-specific swap not being hidden by the using declaration, and refers reader to §18.2.3:

我阅读了该部分,并意识到这可能与依赖于参数的查询(ADL)有关.以下是摘录:

I read that section and realized that this may be related to Argument-Dependent Lookup (ADL). The following is the excerption:

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

But I still have some vagueness in the understanding. My question is: does the ADL go before the normal scope lookup, or after the normal scope lookup? My current understanding is that ADL goes before the normal scope lookup, because otherwise it should be the std::swap that is used. I need confirmation if you think I am right, or please point out what mistake I've made if you think I am wrong. Thank you.

推荐答案

ADL 以前没有走过,不是特别喜欢的方法;除了通过常规名称查找找到的名称之外,还将考虑使用ADL找到的名称.

ADL doesn't go before, it's not preferred specially; the names found by ADL will be considered in addition to the names found by the usual name lookup.

除了通常的非限定名称查找所考虑的范围和名称空间之外,还在其参数的名称空间中查找这些函数名称.

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找到的所有命名和常规名称查找决议;那么将选择最佳匹配.

That means all the named found by ADL and usual name lookup will be considered in overload resolution; then the best match will be selected.

为了编译函数调用,编译器必须首先执行名称查找,对于函数而言,名称查找可能涉及依赖于参数的查找,对于函数模板,可能要遵循模板参数推导.如果这些步骤产生了多个候选函数,那么将执行重载解析以选择将实际调用的函数.

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.

这篇关于依赖于参数的查找是否在普通范围查找之前进行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆