home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13231 < prev    next >
Encoding:
Text File  |  1992-09-02  |  3.6 KB  |  73 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.mentorg.com!sdl!adk
  3. From: adk@Warren.MENTORG.COM (Ajay Kamdar)
  4. Subject: Re: istrstream
  5. Message-ID: <1992Sep2.163001.2107@Warren.MENTORG.COM>
  6. Organization: Mentor Graphics Corp. - IC Group
  7. References: <1992Sep1.225259.1533@Warren.MENTORG.COM> <1814g7INNr59@agate.berkeley.edu>
  8. Date: Wed, 2 Sep 1992 16:30:01 GMT
  9. Lines: 62
  10.  
  11. In article <1814g7INNr59@agate.berkeley.edu> jbuck@forney.berkeley.edu (Joe Buck) writes:
  12. >In article <1992Sep1.225259.1533@Warren.MENTORG.COM> adk@Warren.MENTORG.COM (Ajay Kamdar) writes:
  13. >>Why do istrstream constructors take a char* instead of a const char* ?
  14. >>This either stops me from reading a formatted input string (which I get
  15. >>as a const char*) as an istrstream, or compels me to cast away the const
  16. >>on the string when constructing the istrstream.
  17. >
  18. >This annoys me too.  The reason appears to be that istrstream and
  19. >ostrstream are both derived from strstream, and it is the strstream
  20. >constructor that requires the "char *" argument.  Still, it would be nice
  21. >if this could be avoided.  I despise casting away const, and currently
  22. >this is the only place where I feel compelled to do it.  It still may 
  23. >not be safe, because of putback.
  24. >
  25.  
  26. I guess you meant that istrstream derives from strstreambase and not
  27. strstream, and that the strstreambase has a CTOR which takes a char*
  28. argument.
  29.  
  30. It is unfortunate that the fact that strstreambase has a char* CTOR should
  31. dictate that istrstream should have a char* CTOR too. I would have much
  32. preferred that the istrstream have a const char* CTOR, and that the
  33. istrstream class cast away the const to create the strstreambase part of
  34. it self. Along with casting away the const internally, istrstream would
  35. also set up some other conditions in strstreambase which would ensure that
  36. even though the strstreambase was created with a char* argument, it does not
  37. actually modify the buffer. This would making the cast away of the const
  38. purely an implementation detail of the istrstream class while still
  39. maintaining the constness of its CTOR argument.
  40.  
  41. Such internal casting away of const purely to facilitate implementation
  42. is perectly safe. I use such a technique to implement manipulators
  43. and iterators for containers. I implement my iterators by deriving privately
  44. from the manipulator, casting away the const of the argument in the iterator
  45. CTOR to construct the manipulator, and ensuring in the iterator member
  46. functions (which would be all const functions) that only the const member
  47. functions of the manipultor are ever called from the iterator interface.
  48.  
  49. >Depending on the implementation, I suppose that it is possible that
  50. >istream::putback(char) might modify the argument to the istrstream
  51. >constructor, which could cause trouble if you passed a string argument
  52. >and the code modified the string (possibly causing a bus error if the
  53. >string is in ROM).
  54.  
  55. Note that istream::putback(char) only attempts to put back a character
  56. into the input stream. It is not required to succeed, and in fact if the
  57. character being put back is not the last character extracted, the effect
  58. of putback() is undefined. Hence in case of istrstream, it would be ok
  59. if putback(char c) simply moved the get pointer one character back without
  60. any attempt to write its argument into the stream.
  61.  
  62. Is the standards committee leaning towards an istrstream() which takes
  63. a char* argument? Or is it trying to work things such that it takes a
  64. const char* argument? I would strongly urge it do the latter.
  65.  
  66. - Ajay
  67.  
  68. -- 
  69. I speak for none but myself.
  70.  
  71. Ajay Kamdar                               Email : ajay_kamdar@mentorg.com
  72. Mentor Graphics, IC Group (Warren, NJ)    Phone : (908) 580-0102
  73.