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

c – 为什么在gcc库中声明了相同类型的重复

发布时间:2020-12-16 07:27:44 所属栏目:百科 来源:网络整理
导读:在 gcc库中,模板basic_stringbuf派生自basic_streambuf.在基类basic_streambuf中,已经声明了类型名称,例如char_type,traits_type.为什么在子类basic_stringbuf中声明它们是重复的? 相关代码粘贴在下面. // c++/4.2.1/streambuftemplatetypename _CharT,type
在 gcc库中,模板basic_stringbuf派生自basic_streambuf.在基类basic_streambuf中,已经声明了类型名称,例如char_type,traits_type.为什么在子类basic_stringbuf中声明它们是重复的?

相关代码粘贴在下面.

// c++/4.2.1/streambuf
template<typename _CharT,typename _Traits>
    class basic_streambuf 
    {
    public:
      //@{
      /**
       *  These are standard types.  They permit a standardized way of
       *  referring to names of (or names dependant on) the template
       *  parameters,which are specific to the implementation.
      */
      typedef _CharT                    char_type;
      typedef _Traits                   traits_type;
      typedef typename traits_type::int_type        int_type;
      typedef typename traits_type::pos_type        pos_type;
      typedef typename traits_type::off_type        off_type;
      //@}

      //@{
      /**
       *  @if maint
       *  This is a non-standard type.
       *  @endif
      */
      typedef basic_streambuf<char_type,traits_type>   __streambuf_type;
      //@}
…
};

// c++/4.2.1/sstream
template<typename _CharT,typename _Traits,typename _Alloc>
    class basic_stringbuf : public basic_streambuf<_CharT,_Traits>
    {
    public:
      // Types:
      typedef _CharT                    char_type;
      typedef _Traits                   traits_type;
      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 251. basic_stringbuf missing allocator_type
      typedef _Alloc                        allocator_type;
      typedef typename traits_type::int_type        int_type;
      typedef typename traits_type::pos_type        pos_type;
      typedef typename traits_type::off_type        off_type;

      typedef basic_streambuf<char_type,traits_type>   __streambuf_type;
      typedef basic_string<char_type,_Traits,_Alloc>  __string_type;
      typedef typename __string_type::size_type     __size_type;
…
};

更新:

char_type已在父类中声明为公共成员.子类可以直接使用它.我的问题是为什么gcc不实现basic_stringbuf,如下所示

template<typename _CharT,_Traits>
    {
    public:
      // Types:
      //typedef _CharT                  char_type;
      //typedef _Traits                     traits_type;
      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 251. basic_stringbuf missing allocator_type
      typedef _Alloc                        allocator_type;
      //typedef typename traits_type::int_type      int_type;
      //typedef typename traits_type::pos_type      pos_type;
      //typedef typename traits_type::off_type      off_type;

      typedef basic_streambuf<char_type,_Alloc>  __string_type;
      typedef typename __string_type::size_type     __size_type;
…
};

编辑:

谢谢K-ballo.我认为你的答案是有道理的.我试过下面的代码.类型名称char_type不能在子类中使用.

template<typename _CharT>
class Base
{
public:
    typedef _CharT char_type; 
};

template<typename _CharT>
class Child : public Base<_CharT>
{
private:
    char_type _M_Data; // FAIL: Unknown type name 'char_type'
};

解决方法

你试过这样做吗?基类basic_streambuf< _CharT,_Traits>是一个依赖类型,编译器将不知道basic_streambuf(如果有)的基础是什么,直到实际类型被实例化.因此,基类的定义在派生类中不可见.为了使在依赖基类中声明的类型可见,通常使用typedef typename base_class :: some_type some_type ;,gcc决定再次重新定义它们,就像基数一样.

它简化为char_type虽然在父类中声明为公共成员,但不能由子类直接使用.它必须用它作为

typename basic_streambuf< _CharT,_Traits >::char_type

(编辑:李大同)

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

    推荐文章
      热点阅读