home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16179 < prev    next >
Encoding:
Text File  |  1992-11-12  |  2.0 KB  |  56 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Just what is so great about streams?
  5. Message-ID: <1992Nov12.175553.8218@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Nov12.061942.16473@nuscc.nus.sg>
  8. Date: Thu, 12 Nov 1992 17:55:53 GMT
  9. Lines: 45
  10.  
  11. tim@gold.iss.nus.sg (Tim Poston) writes:
  12.  
  13. >This posting is a genuinely answer-seeking question,
  14.  
  15. >  what _is_ so great about streams?
  16.  
  17. Streams are not the best solution for all I/O or formatting problems.
  18. Streams provide useful solutions to common I/O and formatting problems.
  19.  
  20. Streams provide type safety, generalization of I/O, and user
  21. extensibility.  These features are not available in C stdio.
  22.  
  23. 1. Type safety:  Printf and scanf provide no verification that the
  24. format codes match in number or type the parameters passed.  This is
  25. frequent cause of program failures which are hard to find.  More
  26. subtly, there are no formatting codes which portably correspond to
  27. the standard types size_t, ptrdiff_t, or wchar_t:
  28.     size_t x;    // is it sizeof int or sizeof long?
  29.     scanf("%u", &x); // might work, might give wrong anwser
  30.     scanf("%lu", &x); // might work, might trash the stack
  31. Portable code has to work around this problem.
  32.  
  33. 2. Generalization of I/O:  If 's' is (derived from) an ostream, you can write
  34.     s << foo;
  35. without knowing or caring whether it is a file, a string, or some
  36. other kind of device.  You have to use separate calls based on
  37. external information with stdio.
  38.  
  39. 3. User extensibility:  You can define your own "shift" operators for
  40. your own types, and do I/O without the need for special function calls:
  41.  
  42.     typedef ... T;
  43.     ostream& operator<<(ostream&, const T&);
  44.     istream& operator>>(istream&, const T&);
  45.     ...
  46.     extern T t;
  47.     cout << t;
  48.     cin >> t;
  49. The last two statements continue to work unchanged even if the type of
  50. T is changed.  (You could get comparable functionality with overloaded
  51. I/O functions in C++ using stdio internally instead of streams.)
  52. -- 
  53.  
  54. Steve Clamage, TauMetric Corp, steve@taumet.com
  55. Vice Chair, ANSI C++ Committee, X3J16
  56.