Boost logo

Boost :

From: Dan Nuffer (dnuffer_at_[hidden])
Date: 2001-06-29 13:25:52


Samuel Krempp wrote:
>
> A year ago, Responding to David Abrahams' challenge in msg 2379, a format
> class was implemented by Rüdiger Loos's, placed in the vault (as
> 'format') and discussed for some time.
>
> Then (in msg 4582) Karl Nelson brought in his own implementation of such a
> class, with different orientation
> (http://www.ece.ucdavis.edu/~kenelson/ofrstream.cc)
> Discussions followed, and msg 4634 presents a summary of the issues by
> karl nelson.
>
> Now, looking at those 2 classes, I implemented my own format class
> and uploaded it in the vault as format2, and I'd like to get
> comments on it, and eventually proceed to a formal review.
>
> I uploaded (in msg.txt) a presentation to this class that I copy here,
> but it is not intended as an introduction and the sample program (and the
> header) are best read first.
>
> -------
> Usage :
> -------
> format( format_string ) arg1 arg2 arg3 ..
> where each argument has the form :
> [ '*' mod1 '*' mod2 '*' ... '*' modN] '%' var
>
> mod1 .. modN are treated as stream manipulators,
> That is : they are sent to the stream but dont count as an argument,
> and their (potential) string output in the stream is discarded.
>
> - there must be enough argument to satisfy the format string
> Note format(" %12$s ") % .. is legal (if followed by 12 arguments)
> whereas it is forbidden in Unix98 printf
>
> - extra arguments are ignored (but are evaluated, of course)
>

Samuel,
I'm glad that someone is taking up work on format again!
I've been using a modified version of format that's in the vault quite
heavily in a project that I'm working on (OpenWBEM). The program is a
networking daemon, so almost all uses of format are to print log
messages and create error messages.

Here are some of my opinions about your proposal.
I find the usage:
format( format_string, arg1, arg2, ... )
to be more flexible than what you have proposed. Most of the time that
I use the format class, it isn't being sent to a stream. I've put an
"operator string" and a "const char* c_str() const" functions on it to
allow it to be passed to the logger and in constructors of exceptions.
It still has an "operator <<" to allow it to be inserted into streams,
if need be.

I also like the fact that I don't have to worry about the type of the
argument, all I've got to do is put %1, %2, etc. Granted, this is not
as flexible as far as formatting goes, but I have not once needed to do
fancy formatting on the arguments. I always hated using printf because
I usually had to look at the documentation to remember what to put after
the %. I think it should be possible to support simply %1, %2, as well
as all the printf stuff.

Thanks,
Dan Nuffer


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