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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: Just what is so great about streams?
  5. In-Reply-To: pat@frumious.uucp's message of Sat, 14 Nov 1992 01:47:33 GMT
  6. Message-ID: <KANZE.92Nov17192153@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <1992Nov12.061942.16473@nuscc.nus.sg>
  10.     <KANZE.92Nov13200446@slsvhat.us-es.sel.de>
  11.     <1992Nov14.014733.534@frumious.uucp>
  12. Date: 17 Nov 92 19:21:53
  13. Lines: 44
  14.  
  15. In article <1992Nov14.014733.534@frumious.uucp> pat@frumious.uucp
  16. (Patrick Smith) writes:
  17.  
  18. |> kanze@us-es.sel.de (James Kanze) writes:
  19. |> |Does anyone know of a better way of handling IO formatting in a
  20. |> |type-safe, extensible manner.  It's too late to change the iostream
  21. |> |syntax, but I am curious as to what the alternatives are.
  22.  
  23. |> I've always thought it would be nice to be able to write
  24. |> something like
  25.  
  26. |>    int i = 20, j = 17;
  27. |>    cout["i = %d, j = %d\n"] << i << j;
  28.  
  29. |> This wouldn't involve any loss of type safety, since a separate
  30. |> routine would be invoked for each type of value to be printed.
  31. |> With some work, it could probably also be set up so that it
  32. |> could be extended to printing user-defined types.
  33.  
  34. This is great.  The really nice thing about it is that it is a natural
  35. extention to the current streams library; it doesn't break anything
  36. and it doesn't really add still another idiom.  I'd probably put the
  37. format string in parentheses, though, and overload the operator(), but
  38. that's just nit-picking.
  39.  
  40. What is really nice is that it can do automatic type conversions,
  41. which even printf couldn't, ie:
  42.  
  43.     cout( "i = %f , j = %f\n" ) << 0 << 1 ;
  44.  
  45. Typically, once I got used to printf, the only place I regularly
  46. fouled up is when I had a variable with the format (which was the same
  47. for a number of printf's), and at some point, wanted to use a constant
  48. (like 0) instead of a variable.  Inevitably, I'd forget that it had to
  49. be 0.0.
  50.  
  51. Here, of course, the overloaded operator( fmtstream& , int )
  52. recognizes that the requested format is %f, converts the int to
  53. double, and passes it to operator( fmtstream& , double ).
  54. --
  55. James Kanze            GABI Software, Sarl.
  56. email: kanze@us-es.sel.de    8 rue du Faisan
  57.                 67000 Strasbourg
  58.                 France
  59.