朋友访问受保护的嵌套类 [英] Friend access to protected nested class
问题描述
我有以下C ++代码:
I have the following C++ code:
class A {
protected:
struct Nested {
int x;
};
};
class B: public A {
friend class C;
};
class C {
void m1() {
B::Nested n; // or A::Nested
}
};
使用g ++ 4.4编译此代码段,无论我使用B :: Nested还是A ::嵌套在m1中。 Clang接受 B :: Nested
,但是如果我 A :: Nested
,则不会编译。
Compiling this snippet with g++ 4.4, it does not make a difference whether I use B::Nested or A::Nested in m1. Clang accepts B::Nested
, but does not compile if I A::Nested
. Is that a bug in g++ or in clang?
推荐答案
根据标准,GCC是正确的,Clang是错误的。它表示在11.2 / 4
According to the Standard, GCC is correct and Clang is wrong. It says at 11.2/4
成员m在N类中命名时可访问,如果
A member m is accessible when named in class N if
- m被保护,并且引用发生在N类的成员或朋友中,或在从N派生的类P的成员或朋友中,其中m P的成员是私人或受保护的
这是Clang bugreport的主题,从建立Qt: http://llvm.org/bugs/show_bug.cgi?id=6840 。一个ang人说
This is subject of this Clang bugreport, which prevents Clang from building Qt: http://llvm.org/bugs/show_bug.cgi?id=6840 . One Clang guy says
其实,我有意没有实现这个规则。这是一个
起草错误或一个可怕的错误。它中断了整个protected
说明符,它使代码的良好形式依赖于
存在完全不相关的类,它对
实现了高成本,并且它是正式不可判定的在存在模板的情况下。
Actually, I intentionally haven't implemented this rule yet. It is either a drafting error or a horrible mistake. It neuters the entire 'protected' specifier, it makes the well-formedness of code dependent on the existence of completely unrelated classes, it imposes high costs on the implementation, and it's formally undecidable in the presence of templates.
这篇关于朋友访问受保护的嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!