_M_fill initialization in basic_ios::init

Richard Burkert rrburkert@pobox.com
Sat Dec 1 19:35:00 GMT 2001


  Hi,
Here's basic_ios::init from basic_ios.tcc for reference...

  template<typename _CharT, typename _Traits>
    void
    basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
    {
      // NB: This may be called more than once on the same object.
      ios_base::_M_init();
      _M_cache_facets(_M_ios_locale);
      _M_tie = 0;
      _M_fill = this->widen(' ');
      _M_exception = goodbit;
      _M_streambuf = __sb;
      _M_streambuf_state = __sb ? goodbit : badbit;
    }

See how _M_fill is initialized unconditionally.  But if the locale didn't
support ctype for _CharT then _M_cache_facets would skip initialization
of _M_ios_fctype and you'd have problems when widen chains into
_M_ios_fctype->widen.  In such conditions formatting shouldn't be
relevant, so _M_fill wouldn't need to be initialized and you could just
stick an
      if (has_facet<__ctype_type>(__M_ios_locale))
above the initialization in init, or else put the initialization inside the
similar if statement in _M_cache_facets. If you put it in _M_cache_facets
it would also get reset to something appropriate when imbue is called,
otherwise the fill character is theoretically meaningless after a call
to imbue since it remains at whatever it was with the previous locale.

I only noticed because I was segfaulting inside widen on creation of
a stream of ints i'd set up.  So yes it might happen =P

Rich



More information about the Libstdc++ mailing list