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