From: kanze@us-es.sel.de (James Kanze)
Newsgroups: comp.lang.c++
Subject: Re: Just what is so great about streams?
Date: 19 Nov 92 01:22:21 GMT
Article-I.D.: slsvdnt.KANZE.92Nov18172221
	<KANZE.92Nov17192153@slsvdnt.us-es.sel.de>
	<1992Nov17.231038.66501@Cookie.secapl.com>
Organization: SEL

In article <1992Nov17.231038.66501@Cookie.secapl.com>
frank@Cookie.secapl.com (Frank Adams) writes:

|> 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.

Next question: how do you pass the formatting parameters to the
object's formatter?  Ideally, I would write:

	cout( "x == %6.3g\n" ) << myClassVariable ;

and the formatter for myClassVariable would have some way of getting
at the 6 and the 3.

As an idea: the formatter is in fact an overload of:

	fmtstream& operator<<( fmtstream& out , const MyClass& value ) ;

So it should be possible to set these values somehow in the fmtstream,
and access them through the first parameter of the overloaded
operator. 

Also, could derivation be used in some way to allow a class that can't
really do anything with the formatting information to only overload
the ostream form, an that this would be used for the formatting if the
fmtstream overload was not available?  Note that the overloaded
operator<< cannot be a member function, since it must be written by
the author of the class to be output, and thus, it cannot be virtual.

|> 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.
--
James Kanze			GABI Software, Sarl.
email: kanze@us-es.sel.de	8 rue du Faisan
				67000 Strasbourg
				France
Newsgroups: comp.lang.c++
From: nikki@trmphrst.demon.co.uk (Nikki Locke)