Boost logo

Boost :

From: Samuel Krempp (krempp_at_[hidden])
Date: 2001-12-20 03:48:37


On Thu, 2001-12-20 at 02:52, Karl Nelson wrote:

> If you want to force rules of presidence use what is given for
> that...
>
> cout << (format(str) << 1 << 2) << endl;
>
> The class could be designed to handle that.

It could, but then in those cases it would act like a string-maker, not
a manipulator.
(inside the parenthesis, format doesn't know about cout's state. So it
does its formatting, and then dump a string at the end. There is no way
to stack the arguments and wait until you get cout, before formatting
them)
So format would have a different behaviour there, and it would not be
apparent.
So all in all, I guess you would not make the class handle that, unless
it always act like a string-maker like mine.

In fact, I realised you use operator<< because you aim at making format
pretty much like a manipulator.
while my class is a string maker , for which operator<<(stream, format)
is define to facilitate the use inside stream output chains.

that's the heart of the issue..
In my experiments, it seemed to me it wasn't possible to make the format
objects act like a manipulators in all aspects, I'd always get a dirty
aspect sooner or later. I'll try to recall the details for this evening,
to debate whether we should stick to a simple string-maker or aim for a
'stream manipulator' approach. This choice governs the choice between
<< and %.

> > hum, the user forgot one of the 2 arguments (raise an exception, no ?)
> > but I don't see the link with what I tried to say.
>
> How is the execption to be handled? What good does it do to raise it?
> Can you provide a reasonable code where raising an exception when
> strings are being provided from an outside source will make
> a critical difference to the coder?

When an exception is raised, the developper has a chance of noticing the
error, and correct it, instead of leaving it in the released code, where
it will sooner or later be a nuisance on the poor user.
But it's true that those exceptions should be possible to switch off.
I can do that like stream's eceptions are handled.

> > > cout << hex << format("%d") << 16;
> > Who could guess that hex won't have an effect,a nd that it will print
> > "16" instead of "10" ??
>
> the %d should be a clue. :-)

Right :-)
What about
cout << hex << format("%s") << 16;
or whatever directive you treat without modifying the stream state.
Your format prints 10, but if you put the format inside parenthesis, it
prints 16

regards

-- 
Samuel

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk