且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-08 19:40:27

依赖于参数的查找是找到 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).