来自iso c++ n3290的一点:参数依赖的名称查找:

A point from iso C++ n3290 : Argument dependant Name Lookup:

本文关键字:依赖 参数 查找 一点 c++ iso n3290 来自      更新时间:2023-10-16

来自iso c++ n3290的一个点:参数依赖的名称查找:第3.4.2节,第4段

When considering an associated namespace, the lookup is the same as the lookup
performed when the associated namespace is used as a qualifier (3.4.3.2) except
 that:
 — Any using-directives in the associated namespace are ignored.
 — Any namespace-scope friend functions or **friend function templates** declared
   in associated classes are visible within their respective namespaces even if 
   they are not visible during an ordinary lookup (11.3).
 — All names except those of(possibly overloaded) functions and function 
    templates are ignored.

与2003年的标准相比,他增加了第三点。谁能解释一下……用示例解释....(重载)..

他还说,在第二点中,他包括了友元函数模板(我知道norml类友元函数)。

我认为查找应该一直这样工作,更多的是澄清而不是实际的变化。我不确定是否添加了它,因为其他地方添加了一些其他措辞,需要使这更清楚,编译器实际上在一些角落情况下有所不同,或者一些机构质疑什么是正确的实现。

Ad点2。就像你可以声明函数f是类c的友元一样,你也可以声明函数模板t是类c的友元。声明是不同的,因为它显式地提到了模板参数,所以他们觉得有必要显式地适用于两种情况。

template <typename T> bool f(T);
class c {
    friend bool f<c>(c); // only particular instantiation is friend
    template <typename T> friend bool f<T>(T); // all instantiations are friends
}

(当然,您可以将它与c作为模板本身结合起来,以获得无限的乐趣)。

广告点3。该子句意味着,如果在包含类f的名称空间n中查找函数f,则不会考虑类f(而如果编写n::f,则会取该类)。"可能超载"并不一定要出现。函数总是可以重载的,并且在所有命名空间中发现的所有重载都将包含在最终的重载解析中。

namespace n {
    class c { ... };
    class e { ... } f;
}
namespace o {
    class d { ... };
    void f(c &, d &) { ... };
    void f(c &, d &, bool) { ... };
}
namespace p {
    f(c(), d());
}

f(c(), d())关联的命名空间同时为no。然而,n::f不是一个函数,而是一个(可能是函子)实例,因此不考虑它(而旧的措辞允许考虑foperator())。o::f被重载,所有的重载都被考虑(并且,在收集了f的所有可能的含义之后,3参数的变体被排除,因为只给出了2个参数)。