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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!agate!doc.ic.ac.uk!mrccrc!warwick!bham!bhamvx!mccauleyba
  3. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  4. Subject: Re: Just what is so great about streams?
  5. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  6. Message-ID: <1992Nov17.204453.1@vax1.bham.ac.uk>
  7. Date: Tue, 17 Nov 1992 20:44:53 GMT
  8. Lines: 43
  9. References: <1992Nov12.061942.16473@nuscc.nus.sg> <1992Nov12.171641.13797@aplcen.apl.jhu.edu> <24184@alice.att.com> <mg.721969238@tyrolia>
  10. Organization: University of Birmingham
  11.  
  12. In article <mg.721969238@tyrolia>, mg@tyrolia (Michael Golan) writes:
  13. > Any good reason why
  14. >   cout << fmt("%5d %5d\n") << x << y ;
  15. > or even
  16. >   cout("%5d,%5d\n") << x << y ;
  17. > do not exist?
  18. Yes - nobody has written them.
  19.  
  20. To implement ostream::operator()() would of course require modification to
  21. the ostream class and we'd like to avoid that.
  22.  
  23. A better implementation goes as follows:
  24.  
  25. struct fmt { const char *form; fmt(const char *f) : form(f) {}};
  26.  
  27. struct formstream {
  28.   ostream& os; const char *form;
  29.   formstream(ostream& o,const char *f) : os(o),form(f) {} 
  30.   preprocess(); postprocess();
  31. }
  32.  
  33. formstream operator<<(ostream& o,fmt& f) {return formstream(o,f.form);} 
  34.  
  35. template <class T>
  36. formstream& operator(formstream& f,const T& t) {
  37.   f.preprocess(); f.os << t; f.postprocess(); return f;
  38. }
  39.  
  40. OK I've given you a start now do you want to try to implement it?
  41.  
  42. > Also, the book fails to mention that cin.get(c) is slower than getchar()
  43. > by a factor of 2 or so. Same for fread vs. cin.read(), at least in the
  44. > simple timing I made under BC++3.0. This is a serious problem, e.g., when
  45. > you read a 32k chunks of bin data into memory. 
  46. It may be a silly question but are you sure you have in-lining switched
  47. on?
  48. -- 
  49.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  50.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  51. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  52. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  53.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  54. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  55.