[RFC] Default constructed stringbufs and stringstreams

Paolo Carlini pcarlini@suse.de
Tue Sep 28 23:51:00 GMT 2004


Hi everyone, hi Nathan,

I'm working on these ideas related to the short-string optimization and
just noticed 27.7.1.1/2, about basic_stringbuf default constructor: "The
function allocates no array object".

This has interesting implications, since our basic_stringbuf has a 
basic_string
*data member*!

Our current implementation *seems* ok, since, thanks to the empty string 
optimiz
we are not allocating anything, but implies, for instance, that in the 
seeks we
check that capacity != 0 (i.e., we don't look at the pointers, see 
below) and we
cannot tell a default constructed stringbuf from one constructed from an 
empty
basic_string.

If/when we have a short-string inside the string object, a default 
constructed
string will acquire a non-zero capacity, the implementation of 
basic_stringbuf
will be still possible, in principle, since, again, we don't allocate 
anything.

Now one of my (many) doubts: due to 27.7.1.1/2 shouldn't a default 
constructed
stringbuf have null get/put area pointers? In our current implementation 
this
is not happening and I believe we have a bug. This seems confirmed by 
27.7.1.1/1
which says that the base class is initialized by basic_streambuf() 
(27.5.2.1)

And there are also interesting implications for DR 453.

Lot of work for tomorrow, I guess... ;)

Paolo.

//////////

#include <sstream>
#include <cassert>

class my_stringbuf : public std::stringbuf
{
public:
  const char_type*
  pub_eback() const
  { return eback(); }

  const char_type*
  pub_gptr() const
  { return gptr(); }

  const char_type*
  pub_egptr() const
  { return egptr(); }

  const char_type*
  pub_pbase() const
  { return pbase(); }

  const char_type*
  pub_pptr() const
  { return pptr(); }

  const char_type*
  pub_epptr() const
  { return epptr(); }
};

void test01()
{
  my_stringbuf sbuf;

  assert( !sbuf.pub_eback() );
  assert( !sbuf.pub_gptr() );
  assert( !sbuf.pub_egptr() );

  assert( !sbuf.pub_pbase() );
  assert( !sbuf.pub_pptr() );
  assert( !sbuf.pub_epptr() );
}

int main()
{
  test01();
  return 0;
}



More information about the Libstdc++ mailing list