在类中定义的友元函数的完全限定名称是什么? [英] What is the fully qualified name of a friend function defined inside of a class?

查看:30
本文介绍了在类中定义的友元函数的完全限定名称是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类中定义的友元函数的完全限定名是什么?

What is the fully qualified name of a friend function defined inside of a class?

我最近看到了一个类似于以下的示例.下面val()的全称是什么?

I recently saw an example analogous to the following. What is the fully qualified name of val() below?

#include <iostream>

namespace foo {
    class A {
        int x;
    public:
        A(int x = 0) : x(x) { }

        friend int val(const A &a) { return a.x; }
    };
}

int main() {
    foo::A a(42);

    // val() found using ADL:
    std::cout << val(a) << std::endl;

    // foo::val(a); // error: 'val' is not a member of 'foo'
    // foo::A::val(a); // error: 'val' is not a member of 'foo::A'

    return 0;   
}

依赖于参数的查找是找到 val() 的唯一方法吗?

Is argument-dependent lookup the only way val() can be found?

诚然,这并非源于实际问题.我只是希望获得更好的理解.

Admittedly, this does not stem from a practical problem. I am simply looking to gain a better understanding.

推荐答案

依赖于参数的查找是找到 val() 的唯一方法吗?

Is argument-dependent lookup the only way val() can be found?

是的,这是唯一的方法.引用 [namespace.memdef]/3 的神圣标准:

Yes, it is the only way. To quote the holy standard at [namespace.memdef]/3:

如果非本地类中的友元声明首先声明了一个类,朋友是成员的函数、类模板或函数模板最里面的封闭命名空间.朋友声明不本身使名称对非限定查找或限定查找可见.

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. The friend declaration does not by itself make the name visible to unqualified lookup or qualified lookup.

因此,虽然 valfoo 的成员,但仅从朋友声明中查找是不可见的.需要一个类外定义(这也是一个声明)才能使其可见.对于内联定义(并且没有类外声明),这意味着 ADL 是调用函数的唯一方法.

So while val is a member of foo, it's not visible to lookup from the friend declaration alone. An out of class definition (which is also a declaration) is required to make it visible. For an inline definition (and no out-of-class declaration) it means ADL is the only way to call the function.

作为一个额外的好处,C++ 确实曾经有一个朋友名字注入"的概念.然而,这已被删除,并调整了 ADL 的规则作为替代.可以在 WG21 论文 N0777 中找到更详细的概述 (pdf).

As an added bonus, C++ did once have a concept of "friend name injection". That however has been removed, and the rules for ADL adjusted as a replacement. A more detailed overview can be found in WG21 paper N0777 (pdf).

这篇关于在类中定义的友元函数的完全限定名称是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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