【发布时间】:2016-09-26 00:53:34
【问题描述】:
同样的问题被问到:为什么 GCC 允许从私有嵌套类继承? 对于非模板类,它允许从私有嵌套类继承, 如果是朋友,但不是模板类。 是bug吗?
template<class Base>
class InheritFromBaseMember : public Base::MemberPrivate // error
{
using PrivateMember = typename Base::MemberPrivate; // works fine
};
class MyBase{
friend class InheritFromBaseMember<MyBase>;
// another try to declare it friend
template<class T>
friend class InheritFromBaseMember;
friend class AnotherClass;
class MemberPrivate{};
};
class AnotherClass : public MyBase::MemberPrivate{}; // works fine
int main() {
InheritFromBaseMember<MyBase>{};
}
来自 g++ 5.3.0 的错误消息:
error: 'class MyBase::MemberPrivate' is private
class MemberPrivate{};
^
error: within this context
class InheritFromBaseMember : public Base::MemberPrivate // error
^
【问题讨论】:
-
您确定编译器版本吗?它使用 g++ 4.9.0 及更高版本(包括 5.3.0)编译 here 就好了。但不是早期版本。
-
嗯,我用 Eclipse/CDT Cygwin Miscellaneous --version 翻译,输出是:g++ (GCC) 5.3.0
标签: c++ templates inheritance nested friend