gpt4 book ai didi

c++ - 来自 iso C++ n3290 : Argument dependant Name Lookup: 的一个点

转载 作者:行者123 更新时间:2023-11-30 04:33:26 25 4
gpt4 key购买 nike

来自 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 年早些时候的标准相比,他添加了第三点。任何人都可以解释这是怎么可能的......用一个例子来解释....(过载)..

而且他还说在第二点中他包括了友元函数模板(我知道 noraml calss 友元函数)..任何人都可以解释这就是展示的那个陈述。

最佳答案

我认为查找应该始终如此工作,并且更多的是澄清而不是实际更改。我不确定它是否被添加,因为在其他地方添加了一些其他措辞需要使这一点更清楚,编译器实际上在某些极端情况下有所不同,或者一些机构质疑什么是正确的实现。

广告点 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 的 namespace 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 个参数)。

关于c++ - 来自 iso C++ n3290 : Argument dependant Name Lookup: 的一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6856913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com