关注this question about multiple (virtual) inheritance,我想询问一个简单的MWE,它使 g ++ 5.2.0 感到不安,而 clang ++ 3.6.2 处理它很好,即使设置了-Wall
和-Wextra
,也没有任何投诉。所以这是MWE:
class Z {};
class A : virtual Z { protected: A() {} };
class B : virtual Z { protected: B() {} };
class C : A, B { public: C() : A{}, B{} {} };
int main() { C c{}; return 0; }
与clang ++不同,g ++抱怨如下:
gccodd.c++: In constructor ‘C::C()’:
gccodd.c++:2:34: error: ‘A::A()’ is protected
class A : virtual Z { protected: A() {} };
^
gccodd.c++:4:39: error: within this context
class C : A, B { public: C() : A{}, B{} {} };
^
gccodd.c++:3:34: error: ‘B::B()’ is protected
class B : virtual Z { protected: B() {} };
^
gccodd.c++:4:39: error: within this context
class C : A, B { public: C() : A{}, B{} {} };
^
使用旧表单替换C的构造函数中的统一初始化工作正常,但clang ++和g ++都对以下内容感到满意:
class C : A, B { public: C() : A(), B() {} };
这产生了两个明显的选择:
如果是投票问题,(1)可能会获胜,因为 icpc 15.0.0 表示如下:
gccodd.c++(4): error #453: protected function "A::A()" (declared at line 2) is not accessible through a "A" pointer or object
class C : public virtual A, public virtual B { public: C() : A{}, B{} {} };
^
gccodd.c++(4): error #453: protected function "B::B()" (declared at line 3) is not accessible through a "B" pointer or object
class C : public virtual A, public virtual B { public: C() : A{}, B{} {} };
^
那么,是(1)还是(2)?如果是前一种情况,那我的MWE有什么问题?