home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16516 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  3.8 KB

  1. Path: sparky!uunet!ogicse!emory!sol.ctr.columbia.edu!ira.uka.de!slsvaat!josef!kanze
  2. From: kanze@us-es.sel.de (James Kanze)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Just what is so great about streams?
  5. Message-ID: <KANZE.92Nov18172221@slsvdnt.us-es.sel.de>
  6. Date: 19 Nov 92 01:22:21 GMT
  7. Article-I.D.: slsvdnt.KANZE.92Nov18172221
  8. References: <KANZE.92Nov13200446@slsvhat.us-es.sel.de> <1992Nov14.014733.534@frumious.uucp>
  9.     <KANZE.92Nov17192153@slsvdnt.us-es.sel.de>
  10.     <1992Nov17.231038.66501@Cookie.secapl.com>
  11. Sender: news@us-es.sel.de
  12. Organization: SEL
  13. Lines: 76
  14. In-Reply-To: frank@Cookie.secapl.com's message of Tue, 17 Nov 1992 23:10:38 GMT
  15.  
  16. In article <1992Nov17.231038.66501@Cookie.secapl.com>
  17. frank@Cookie.secapl.com (Frank Adams) writes:
  18.  
  19. |> In article <KANZE.92Nov17192153@slsvdnt.us-es.sel.de> kanze@us-es.sel.de (James Kanze) writes:
  20. |> >In article <1992Nov14.014733.534@frumious.uucp> pat@frumious.uucp
  21. |> >(Patrick Smith) writes:
  22. |> >|> kanze@us-es.sel.de (James Kanze) writes:
  23. |> >|> |Does anyone know of a better way of handling IO formatting in a
  24. |> >|> |type-safe, extensible manner.  It's too late to change the iostream
  25. |> >|> |syntax, but I am curious as to what the alternatives are.
  26. |> >
  27. |> >|> I've always thought it would be nice to be able to write
  28. |> >|> something like
  29. |> >
  30. |> >|>    int i = 20, j = 17;
  31. |> >|>    cout["i = %d, j = %d\n"] << i << j;
  32. |> >
  33. |> >This is great.  The really nice thing about it is that it is a natural
  34. |> >extention to the current streams library; it doesn't break anything
  35. |> >and it doesn't really add still another idiom.  I'd probably put the
  36. |> >format string in parentheses, though, and overload the operator(), but
  37. |> >that's just nit-picking.
  38. |> >
  39. |> >What is really nice is that it can do automatic type conversions,
  40. |> >which even printf couldn't, ie:
  41. |> >
  42. |> >    cout( "i = %f , j = %f\n" ) << 0 << 1 ;
  43. |> >
  44. |> >Typically, once I got used to printf, the only place I regularly
  45. |> >fouled up is when I had a variable with the format (which was the same
  46. |> >for a number of printf's), and at some point, wanted to use a constant
  47. |> >(like 0) instead of a variable.  Inevitably, I'd forget that it had to
  48. |> >be 0.0.
  49. |> >
  50. |> >Here, of course, the overloaded operator( fmtstream& , int )
  51. |> >recognizes that the requested format is %f, converts the int to
  52. |> >double, and passes it to operator( fmtstream& , double ).
  53.  
  54. |> I agree entirely (including that it should be () instead of []).  One
  55. |> addition to printf formats should be made: a format code which accepts an
  56. |> object for formatting (using the standard formatting for the object).
  57. |> Perhaps %g could be extended for this purpose.
  58.  
  59. Next question: how do you pass the formatting parameters to the
  60. object's formatter?  Ideally, I would write:
  61.  
  62.     cout( "x == %6.3g\n" ) << myClassVariable ;
  63.  
  64. and the formatter for myClassVariable would have some way of getting
  65. at the 6 and the 3.
  66.  
  67. As an idea: the formatter is in fact an overload of:
  68.  
  69.     fmtstream& operator<<( fmtstream& out , const MyClass& value ) ;
  70.  
  71. So it should be possible to set these values somehow in the fmtstream,
  72. and access them through the first parameter of the overloaded
  73. operator. 
  74.  
  75. Also, could derivation be used in some way to allow a class that can't
  76. really do anything with the formatting information to only overload
  77. the ostream form, an that this would be used for the formatting if the
  78. fmtstream overload was not available?  Note that the overloaded
  79. operator<< cannot be a member function, since it must be written by
  80. the author of the class to be output, and thus, it cannot be virtual.
  81.  
  82. |> I particularly like the fact that this is just a library extension, not a
  83. |> language extension.
  84.  
  85. |> It would be nice to find some way to extend it to support different
  86. |> formatting codes for class objects.  I think this is doable.
  87. --
  88. James Kanze            GABI Software, Sarl.
  89. email: kanze@us-es.sel.de    8 rue du Faisan
  90.                 67000 Strasbourg
  91.                 France
  92.