home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!ira.uka.de!slsvaat!josef!kanze
- From: kanze@us-es.sel.de (James Kanze)
- Subject: Re: Just what is so great about streams?
- In-Reply-To: pat@frumious.uucp's message of Sat, 14 Nov 1992 01:47:33 GMT
- Message-ID: <KANZE.92Nov17192153@slsvdnt.us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL
- References: <1992Nov12.061942.16473@nuscc.nus.sg>
- <KANZE.92Nov13200446@slsvhat.us-es.sel.de>
- <1992Nov14.014733.534@frumious.uucp>
- Date: 17 Nov 92 19:21:53
- Lines: 44
-
- In article <1992Nov14.014733.534@frumious.uucp> pat@frumious.uucp
- (Patrick Smith) writes:
-
- |> kanze@us-es.sel.de (James Kanze) writes:
- |> |Does anyone know of a better way of handling IO formatting in a
- |> |type-safe, extensible manner. It's too late to change the iostream
- |> |syntax, but I am curious as to what the alternatives are.
-
- |> I've always thought it would be nice to be able to write
- |> something like
-
- |> int i = 20, j = 17;
- |> cout["i = %d, j = %d\n"] << i << j;
-
- |> This wouldn't involve any loss of type safety, since a separate
- |> routine would be invoked for each type of value to be printed.
- |> With some work, it could probably also be set up so that it
- |> could be extended to printing user-defined types.
-
- This is great. The really nice thing about it is that it is a natural
- extention to the current streams library; it doesn't break anything
- and it doesn't really add still another idiom. I'd probably put the
- format string in parentheses, though, and overload the operator(), but
- that's just nit-picking.
-
- What is really nice is that it can do automatic type conversions,
- which even printf couldn't, ie:
-
- cout( "i = %f , j = %f\n" ) << 0 << 1 ;
-
- Typically, once I got used to printf, the only place I regularly
- fouled up is when I had a variable with the format (which was the same
- for a number of printf's), and at some point, wanted to use a constant
- (like 0) instead of a variable. Inevitably, I'd forget that it had to
- be 0.0.
-
- Here, of course, the overloaded operator( fmtstream& , int )
- recognizes that the requested format is %f, converts the int to
- double, and passes it to operator( fmtstream& , double ).
- --
- James Kanze GABI Software, Sarl.
- email: kanze@us-es.sel.de 8 rue du Faisan
- 67000 Strasbourg
- France
-