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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!att-out!walter!qualcom.qualcomm.com!qualcom.qualcomm.com!greg
  3. From: greg@qualcom.qualcomm.com (Greg Noel)
  4. Subject: Re: Can this be done with iostreams?
  5. Message-ID: <1992Nov18.014949.12669@qualcomm.com>
  6. Sender: news@qualcomm.com
  7. Nntp-Posting-Host: qualcom.qualcomm.com
  8. Organization: Qualcomm, Inc., San Diego, CA
  9. References: <1992Nov12.021415.24889@qualcomm.com>
  10. Date: Wed, 18 Nov 1992 01:49:49 GMT
  11. Lines: 56
  12.  
  13. In article <1992Nov12.021415.24889@qualcomm.com> I wrote:
  14. >Here's a fragment of code that I would like to convert to use iostreams.
  15.  
  16. I received several replies to this.  In brief, the answer is that it
  17. can't be done as I'd hoped.
  18.  
  19. Steve Clamage suggested using sprintf to crack the parameters into a
  20. temporary buffer, then write the buffer out.  This would work, but the
  21. intent was to avoid using stdio at all.  Since Steve is the vice-chair
  22. of the ANSI C++ Committee, I'm inclined to accept his pronouncement
  23. that it can't be done the way I wanted.  (He also suggested providing
  24. functions with just those signatures that were actually needed.  Since
  25. this is something that will be used by a lot of different client code,
  26. I don't think I can predict in advance all the cases needed.)
  27.  
  28. Skip Carter sent some rather amazing code that I'm still not sure I
  29. understand.  (Can you really _do_ that?)  Anyway, it depends upon an
  30. explicit `finisher' to indicate when the message was completed, which
  31. is what I wanted to avoid---I wanted the `finisher' to be automatically
  32. invoked at the end of the statement.  If it helps, I essentially want
  33. something whose scope is a single statement, where the constructor and
  34. destructor are invoked immediately before and after the contents of the
  35. statement.
  36.  
  37. This is very similar to the suggestion for a `guard' construct that
  38. just showed up in another thread.  In fact, the number of guises under
  39. which this has shown up indicates to me that this is something that
  40. is lacking from the language---Stuart MacMartin sent a note where
  41. described a case where he would like to have a feature like this to
  42. control debugging output.  And I can think of other cases where I
  43. would like to gain control after doing a sequence of operations on
  44. a single object.
  45.  
  46. Eventually, as I was snooping around the iostreams header looking to
  47. see if there were any hints, I noticed that there were member functions
  48. named `form' and `vform'.  I can't find any documentation about them,
  49. so I suspect that they are not standard (is this the case?), but I can
  50. suffer with them.  Obviously, they are neither type-safe nor extensible,
  51. but I think they will get the job done well enough.
  52.  
  53. Using these functions, the code fragment becomes:
  54.     void ReadRecords::Err(char *msg, ... )
  55.     {
  56.         va_list ap;
  57.         cout << "Line " << linenumber << " of `" << filename << "': ";
  58.         va_start(ap, msg);
  59.         cout.vform(msg, ap) << endl;
  60.         va_end(ap);
  61.         ++errorcount;
  62.     }
  63. I think ``cout << vform(msg, ap) << endl;'' would also work, but I have
  64. not tried it.
  65.  
  66. Thanks for all the help; I appreciate it.
  67. -- 
  68. -- Greg Noel, Unix Guru         greg@qualcomm.com  or  greg@noel.cts.com
  69.