Why is the same type duplicated in the gcc library

In the gcc library, the basic_stringbuf template comes from basic_streambuf. In the base class basic_streambuf, type names such as char_type, traits_type are already declared. Why are they duplicated in the child class basic_stringbuf?

The corresponding code will be inserted below.

// 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; … }; 

Update:

char_type is already declared in the parent class as a public member. Kids can use it directly. My question is why gcc does not implement basic_stringbuf as below

 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; … }; 

EDIT:

Thanks, K-ballo. I think your answer makes sense. I tried the code below. The type name char_type cannot be used in the child class.

 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' }; 
+4
source share
1 answer

Have you tried to do this? The base class basic_streambuf<_CharT, _Traits> is a dependent type, the compiler will not know what specialization basic_streambuf (if any) this database will have until an instance of the real type is created. Because of this, the definitions for the base class are not displayed in the derived class. To make a type declared in a dependent base class visible, typically typedef typename base_class::some_type some_type; , gcc just decided to redefine them again, as the base does.

This boils down to the fact that char_type , although declared in the parent class as a public member, cannot be used directly by the child class. He would have to use it as

  typename basic_streambuf< _CharT, _Traits >::char_type 
+2
source

Source: https://habr.com/ru/post/1380764/


All Articles