home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13641 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  1.8 KB

  1. Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Comma operator for stream I/O (was: Answers on a postcard...)
  5. Message-ID: <4958@holden.lulea.trab.se>
  6. Date: 14 Sep 92 15:26:50 GMT
  7. References: <1992Sep13.160033.7735@taumet.com>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 48
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. This is a side-track from the previous discussion.  Below I propose
  13. an alternate syntax for stream I/O (futile, I know).  I'd be curious
  14. to know what the net thinks about it.
  15.  
  16. steve@taumet.com (Steve Clamage) writes:
  17. : Personally, I've never seen any example of code
  18. : which was improved by using an overloaded comma (which of course
  19. : doesn't mean there is no such code).
  20.  
  21. Since I am also in search of an example where the comma operator would be
  22. a good choice:
  23.  
  24. Would the comma operator have been a good choice for stream I/O?
  25. I.e. instead of the rather hard-to-read expression
  26.  
  27.   cout << "The value is " << value << endl;
  28.  
  29. you would write
  30.  
  31.   cout, "The value is", value, endl;
  32.  
  33. The precedence is just right (very low).
  34.  
  35. The comma operator could be used for input as well:
  36.  
  37.   int i; float f; char c;
  38.   cin, i, f, c;
  39.  
  40. Finally, for iostreams, you would invoke the methods
  41.  
  42.   istream& iostream::in();
  43.   ostream& iostream::out();
  44.  
  45. to select input or output, like this
  46.  
  47.   iostream io(...);
  48.   io.out(), "The value is", value, endl;
  49.   io.in(), i, f, c;
  50.  
  51. Is this technique commonly used in MI classes (base class selector methods,
  52. to do controlled up-casting)?  Just curious...
  53.  
  54. -- 
  55. --------------------------------------------------------------------------
  56. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  57. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  58. --------------------------------------------------------------------------
  59.