Topic: Disappearing constness?


Author: bkline%occs.nlm.nih.gov (Bob Kline)
Date: Tue, 7 Jun 94 21:40:34 GMT
Raw View
We're looking over the 25 Jan 94 draft of the working papers, and we
notice that there is now a constructor for istrstream which takes
a const char * initializer (in fact there are two such constructors,
with and without a second (integer) argument).  The rdbuf() member
function still returns the pointer to the strstreambuf, which in
turn still has str(), which returns a (non-const) char *.  There is
a footnote for this last function, which simply says "The return
value can be a null pointer" without any indication of when this
would happen.  We have also noticed the strstate member of str-
streambuf, which remembers whether the original string was const.
What prevents the user from getting back a non-const pointer to
the original string along these lines?

  const char *p = "Don't tread on me!";
  istrstream is(p);
  char *pp = is.rdbuf()->str();
  *pp = 'X'; // Oops!

Is the flag which remembers that the original string was const
supposed to cause a null pointer to be returned from str() on the
third line?  If so, this isn't clear from the footnote quoted above.
Would it be possible to make the language a little more explicit
here?  Or is this really a hole in the constness of objects handled
by the standard library?

Thanks.

/*----------------------------------------------------------------------*/
/* Bob Kline                                           CSI Technologies */
/* bkline@smtp.csof.com                        Corporate Software, Inc. */
/* voice: (703) 522-0820                            fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/