home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16465 < prev    next >
Encoding:
Text File  |  1992-11-17  |  2.4 KB  |  56 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!secapl!Cookie!frank
  3. From: frank@Cookie.secapl.com (Frank Adams)
  4. Subject: Re: Just what is so great about streams?
  5. Message-ID: <1992Nov17.231038.66501@Cookie.secapl.com>
  6. Date: Tue, 17 Nov 1992 23:10:38 GMT
  7. References: <KANZE.92Nov13200446@slsvhat.us-es.sel.de> <1992Nov14.014733.534@frumious.uucp> <KANZE.92Nov17192153@slsvdnt.us-es.sel.de>
  8. Organization: Security APL, Inc.
  9. Lines: 45
  10.  
  11. In article <KANZE.92Nov17192153@slsvdnt.us-es.sel.de> kanze@us-es.sel.de (James Kanze) writes:
  12. >In article <1992Nov14.014733.534@frumious.uucp> pat@frumious.uucp
  13. >(Patrick Smith) writes:
  14. >|> kanze@us-es.sel.de (James Kanze) writes:
  15. >|> |Does anyone know of a better way of handling IO formatting in a
  16. >|> |type-safe, extensible manner.  It's too late to change the iostream
  17. >|> |syntax, but I am curious as to what the alternatives are.
  18. >
  19. >|> I've always thought it would be nice to be able to write
  20. >|> something like
  21. >
  22. >|>    int i = 20, j = 17;
  23. >|>    cout["i = %d, j = %d\n"] << i << j;
  24. >
  25. >This is great.  The really nice thing about it is that it is a natural
  26. >extention to the current streams library; it doesn't break anything
  27. >and it doesn't really add still another idiom.  I'd probably put the
  28. >format string in parentheses, though, and overload the operator(), but
  29. >that's just nit-picking.
  30. >
  31. >What is really nice is that it can do automatic type conversions,
  32. >which even printf couldn't, ie:
  33. >
  34. >    cout( "i = %f , j = %f\n" ) << 0 << 1 ;
  35. >
  36. >Typically, once I got used to printf, the only place I regularly
  37. >fouled up is when I had a variable with the format (which was the same
  38. >for a number of printf's), and at some point, wanted to use a constant
  39. >(like 0) instead of a variable.  Inevitably, I'd forget that it had to
  40. >be 0.0.
  41. >
  42. >Here, of course, the overloaded operator( fmtstream& , int )
  43. >recognizes that the requested format is %f, converts the int to
  44. >double, and passes it to operator( fmtstream& , double ).
  45.  
  46. I agree entirely (including that it should be () instead of []).  One
  47. addition to printf formats should be made: a format code which accepts an
  48. object for formatting (using the standard formatting for the object).
  49. Perhaps %g could be extended for this purpose.
  50.  
  51. I particularly like the fact that this is just a library extension, not a
  52. language extension.
  53.  
  54. It would be nice to find some way to extend it to support different
  55. formatting codes for class objects.  I think this is doable.
  56.