加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 访问朋友类模板的base子句中的私有定义

发布时间:2020-12-16 06:54:12 所属栏目:百科 来源:网络整理
导读:C中是否合法使类模板B的基类规范依赖于类A的私有定义,它是类模板B的朋友?示例代码: struct Empty {};template typename Tstruct B;struct A{ friend struct BA;private: using Base = Empty;};template typename Tstruct B : T::Base{};int main(){ BA tes
C中是否合法使类模板B的基类规范依赖于类A的私有定义,它是类模板B的朋友?示例代码:

struct Empty {};

template <typename T>
struct B;

struct A
{
    friend struct B<A>;

private:
    using Base = Empty;
};

template <typename T>
struct B : T::Base
{
};

int main()
{
    B<A> test;
    return 0;
}

Godbolt链接:https://godbolt.org/g/HFKaTQ

使用Clang trunk(和旧版本)和MSVC 19(VS 2017)编译代码很好,但无法使用GCC trunk(和旧版本)编译:

test.cpp: In instantiation of 'class B<A>':
test.cpp:21:7:   required from here
test.cpp:15:8: error: 'using Base = class Empty' is private within this context
 struct B : T::Base
        ^
test.cpp:11:20: note: declared private here
  using Base = Empty;
                    ^

哪个编译器错了?

编辑:顺便说一句,如果B转换为常规类(删除模板参数),代码将在GCC中编译.所以,我猜它也应该在类模板的情况下工作.另外cppreference说:“朋友本身也可以从这个类的私人和受保护成员继承.(从C 11开始)”

解决方法

显然这是GCC中的一个错误.提交为 Bug 82613.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读