What is the fully qualified name of a friend function defined inside of a class?(在类中定义的友元函数的完全限定名称是什么?)
问题描述
在类中定义的友元函数的完全限定名是什么?
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.
因此,虽然 val
是 foo
的成员,但仅从朋友声明中查找是不可见的.需要一个类外定义(这也是一个声明)才能使其可见.对于内联定义(并且没有类外声明),这意味着 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).
这篇关于在类中定义的友元函数的完全限定名称是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在类中定义的友元函数的完全限定名称是什么?
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 从 std::cin 读取密码 2021-01-01