home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!secapl!Cookie!frank
- From: frank@Cookie.secapl.com (Frank Adams)
- Subject: Re: Just what is so great about streams?
- Message-ID: <1992Nov17.231038.66501@Cookie.secapl.com>
- Date: Tue, 17 Nov 1992 23:10:38 GMT
- References: <KANZE.92Nov13200446@slsvhat.us-es.sel.de> <1992Nov14.014733.534@frumious.uucp> <KANZE.92Nov17192153@slsvdnt.us-es.sel.de>
- Organization: Security APL, Inc.
- Lines: 45
-
- In article <KANZE.92Nov17192153@slsvdnt.us-es.sel.de> kanze@us-es.sel.de (James Kanze) writes:
- >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 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 ).
-
- I agree entirely (including that it should be () instead of []). One
- addition to printf formats should be made: a format code which accepts an
- object for formatting (using the standard formatting for the object).
- Perhaps %g could be extended for this purpose.
-
- I particularly like the fact that this is just a library extension, not a
- language extension.
-
- It would be nice to find some way to extend it to support different
- formatting codes for class objects. I think this is doable.
-