Boost logo

Boost :

From: Samuel Krempp (krempp_at_[hidden])
Date: 2001-07-02 12:46:57


> Well, leaving aside my dislike of printf :), I still don't think that
> allowance of two _so_ different in complexity and error-proneness forms of
> argument specification is a good decision. IMO, if there is a need for
> supporting extended printf format, a separate 'pformat' class (and only it)
> is a much better way to do it.

I dont think so.
when only the parsing function differ, I use the same class and have a
separate parsing function called.
Having 2 classes is unnecessary code duplication.

> > format formatter("%1 %2 %3 \n");
> > cerr << formatter.print() %1 %2 %3;
> > cerr << formatter.print() %-1 %-2 %-3;
> >
> > assert( (formatter.print() %-1 %-2 %-3).str() == "-1 -2 -3 \n");
> >
> > ( I'll try a loop of those and compare efficiency of this to
> > re-parsing the string each time.. might not be very compelling,
> > but well...)
> >
> > if you dont use print() but applies the arguments directly, the
> > formatter is modified at each call of operator%, so it works only for
> > the first line (normal) Next time, you get an exception as soon as you
> > feed arguments to the formatter.
>
> Ouch! :)

that's not a bug, that's a feature ;-)
( arguments at the bottom )

> [snip]
> > The alternative would be to design a formatter class so that
> > you dont need to call print() :
> > format_launcher printer("%1 %2");
> > cout << printer %x %y;
> > cout << printer %380 %340;
>
> This seems much better to me.
>
> > for this to work, I only need to provide a special operator% to
> > the formatter class, which does :
> > . create a format obect from the data stored in formatter
> > . calls operator% on this format object.
> >
> > But I dont think it is worth the extra code and complexity.
>
> I think it does. I don't like getting exceptions as an answer on something
> that could be catched (or prevented) at compile-time.

on the other hand, you can do
format formatter("%1 : %2\n");
format f2 = formatter %x;
cout << f2.print() %n1;
cout << f2.print() %n2;

which has more flexibility than creating stored formats with only the parsed string,
and no arguments.

the design here is simple, and I think the advantage is that it's very
easy to understand the behaviour of the object, and use it as we see fit.
(giving a few arguments, then others later, etc... )

-- 
Sam

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