朋友困惑

发布于 2024-09-18 05:51:43 字数 607 浏览 2 评论 0原文

$11.4/5 - “[...]类中定义的友元函数位于定义它的类的(词法)范围内[...]”

这句话是什么意思?

struct A{
   typedef int MYINT;
   void f2(){f();}                    // Error, 'f' is undefined
   friend void f(){MYINT mi = 0;}     // Why does this work, shouldn' it be A::MYINT?
   void f1(){f();}                    // Error, 'f' is undefined
};

int main(){}
  1. 这里令人困惑的是,从“A::f1”调用“f”是可以理解的。然而,当朋友处于交友类的“词汇”范围内时,为什么从“A::f2”调用“f”格式不正确? “词法”范围是什么意思?

  2. 同样的类型,为什么在“f”中使用“MYINT”就可以了?难道不应该是 'A::MYINT' 吗?

如果我将“A *”类型的参数添加到“f”,则“f1”和“f2”都可以通过 ADL 找到“f”。这是可以理解的。

$11.4/5 - "[...]A friend function defined in a class is in the (lexical) scope of the class in which it is defined[...]"

What does this statement mean?

struct A{
   typedef int MYINT;
   void f2(){f();}                    // Error, 'f' is undefined
   friend void f(){MYINT mi = 0;}     // Why does this work, shouldn' it be A::MYINT?
   void f1(){f();}                    // Error, 'f' is undefined
};

int main(){}
  1. What is confusing here is that the call to 'f' from 'A::f1' is quiet understandable. However why is call to 'f' from 'A::f2' ill-formed, when a friend is in 'lexical' scope of the befriending class? What does 'lexical' scope mean?

  2. At the same type why is the usage of 'MYINT' in 'f' OK? Shouldn't it be 'A::MYINT'?

If I add a parameter of type 'A *' to 'f', then both 'f1' and 'f2' are able to find 'f' because of ADL. This is understandable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2024-09-25 05:51:43

您仅引用了 §11.4/5 的部分内容。根据它,f() 应该首先在类外声明(函数应该具有命名空间范围)。试试这个:

void f(); // declare it first
struct A{
   typedef int MYINT;
   void f2(){f();}                    
   friend void f(){MYINT mi = 0;}     // definition of global f, a friend of A
   void f1(){f();}                    
};

至于第二个问题,没关系,因为你引用了§11.4/5的部分内容。 f() 遵循与该类的静态成员函数相同的名称绑定规则,并且对封闭类的成员没有特殊的访问权限。

You have quoted only part of §11.4/5. According to it f() should be declared out of class first (function should have namespace scope). Try this:

void f(); // declare it first
struct A{
   typedef int MYINT;
   void f2(){f();}                    
   friend void f(){MYINT mi = 0;}     // definition of global f, a friend of A
   void f1(){f();}                    
};

As for second question, it is ok because of quoted by you part of §11.4/5. f() obeys the same rules for name binding as a static member function of that class and has no special access rights to members of an enclosing class.

孤独患者 2024-09-25 05:51:43

这是我对查询的一部分的解释:

“为什么MYINT”可以称为
“MYINT”而不是“A::MYINT”?

$3.4.1/9 状态 - “名称查找
定义中使用的名称
友元函数 (11.4) 内联定义
在课堂上给予友谊应
按照中的查找描述进行操作
成员函数定义。如果
友元函数没有定义在
授予友谊的班级,姓名查找
在友元函数定义中
应按照查找描述进行
在命名空间成员函数中
定义。”

中,要查找的名称是“MYINT”,这是一个非限定名称。在类中内联定义的友元“f”的定义中查找此名称将在与“A”的成员函数相同。

我的理解正确吗?

Here is my interpreation of one part of my query which is

"Why MYINT" can be referred to as
"MYINT" instead of "A::MYINT"?

$3.4.1/9 states - "Name lookup for a
name used in the definition of a
friend function (11.4) defined inline
in the class granting friendship shall
proceed as described for lookup in
member function definitions. If the
friend function is not defined in the
class granting friendship, name lookup
in the friend function definition
shall proceed as described for lookup
in namespace member function
definitions."

In our case, the name to be looked up is 'MYINT' which is a unqualified name. The lookup for this name inside the definition of the friend 'f' which is defined inline in the class would would be done in the same was as it would be for member functions of 'A'.

Is my understanding correct?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文