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:
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;
source share