|
Boost : |
From: Karl Nelson (kenelson_at_[hidden])
Date: 2001-12-20 16:20:40
>
> On Thu, 2001-12-20 at 02:52, Karl Nelson wrote:
> > > It could, but then in those cases it would act like a string-maker, not
> > > a manipulator.
> >
> > I gave this a bit of thought and I think that format really shouldn't
> > be a manipulator. It should be a method of an ostream...
> >
> > cout.format("There are %d items\n") << i;
> >
> > However, in the face of the fact that I can't get it into
> > standard in that form. I think manipulator is best.
>
> I would love it to be a manipulator. That is, as I said somewhere, the
> ideal. But I have been thinking about this problem a bit, and it's not
> easy (possible?) to make it a true manipulator. You can make it
> something that looks kind of like a manipulator, but isn't, at best.
>
> Consider:
> int x,y;
> cout << format("%2 %1");
> cout << x;
> cout << y;
>
> There's really no way to make this work without changing the way the
> underlying IOstreams work, because you can't avoid calling
> operator<<(ostream &,int), and the only hooks that function lets you
> used are the pretty rigidly constrained locale facets.
Agreed. I would love to be able to make it a real manipulator, but barring
altering stream definition (to something with well planned extensions)
there is no way the above can work.
>
> Now, you can make
>
> cout << format("%2 %1") << x << y;
Okay, then lets approach it a different way then.
format(cout, "%1$s %2$s" ) << x << y;
(which is the only other way I can get cout.format( "%1$s %2$s") << x << y; )
Now there is no question of what it is. It is a function which returns
a stream with reordering. It can throw exceptions for under or
overflow. It obeys stream syntax and rules. No endf is needed.
This is much more clear in my mind than
cout << format("%1 %2") % 1 % 2 << endl;
Which mixes one stream concept with another. Mixed notation like that
is just plain unpleasant to read and introduces another operator which
"acts like" a stream put operator. I made mine a manipulator the
first time just because I did not want to introduce extra concepts
beyond what the C++ programmer knows and operator "%" meaning stream is
exactly that.
--Karl
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk