gpt4 book ai didi

c++ - 友元函数的参数依赖查找

转载 作者:可可西里 更新时间:2023-11-01 18:29:13 24 4
gpt4 key购买 nike

考虑以下几点:

namespace N {
struct A { };

struct B {
B() { }
B(A const&) { }
friend void f(B const& ) { }
};
}

int main() {
f(N::B{}); // ok
f(N::A{}); // error
}

在第一种情况下,情况成功 - 我们考虑 N::B 的关联命名空间并找到 N::f(B const&)。太好了。

第二种情况失败。为什么?根据[namespace.memdef] :

If a friend declaration in a non-local class first declares a class, function, class template or function template the friend is a member of the innermost enclosing namespace. [...] If a friend function or function template is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2).

N::A关联的命名空间是Nf是其中的一个成员,为什么查不到呢?

最佳答案

这是因为 f 没有在相关类中声明。当参数是 B 类型时,B 是一个关联类,但当参数是 A 类型时则不是。

我引用 [basic.lookup.argdep]/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.

关于c++ - 友元函数的参数依赖查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36025007/

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