Boost logo

Boost :

From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2001-07-03 09:50:05


Samuel Krempp write:
> > Not me :). I don't like the idea of full-fledged support of
> > printf format directives at all; conversion characters
> > (type indicators) are unsafe, non-extensible and
> > error-prone at best, they are not technically needed,
>
> in my class, they _are_ type safe.
> cout << format("%p1$x") % n;
> is strictly equiv to
> cout << hex << n;

You mean "is strictly equivalent" to the following, right?

ios_base::fmtflags old_flags(cout.flags());
(cout << hex << n).flags(old_flags);

Ok, so 'x' is not a conversion character or type indicator here, it's just a
shortcuted form of "non-sticky" 'std::hex' manipulator that will be used to
change a state of the output stream for the time the argument it corresponds
to will be printed. What's more, interpretation of 'x' does not depend on
the actual type of the supposedly affected parameter, e.g. if 'n' is an
'std::string' object, or 'my_class' object, the format specification still
won't be ignored, the 'std::hex' manipulator still will be inserted into the
stream, it just won't affect anything (in fact, it may, if 'my_class' is
something like 'std::complex').. Actually, that makes a lot of sense to me
:). Certainly, such interpretation of so printf-like "%p1$x" is not obvious
(and http://groups.yahoo.com/group/boost/files/format2/Readme.txt doesn't
bring a lot of light here, mostly because of intensive mentioning of printf
and printf-compatibility mode without going into details how it really
works), but if you know the rules the whole picture looks quite consistent
and appealing. For example, printf-compatibility mode is a very natural
extension of the described above model, that is based on a simple
observation that, for a limited number of types printf can deal with, and
without any change in the semantics of the 'format' class, the same format
string will result in (almost?) identical results. Two (very) different
interpretations of the same format specifications that give the same result
- I think that's cute :).

[snip]
> 2. beyond printf usual field,
> use its syntax to express formatting option,
> where the flags' meanings are their most natural interpretations :
> example : range = make_pair<int,int>( n1, n2);
> int i;
> cout << format("%p1$4x %2\n") %range %i;
> will do the same as
> cout << hex << setw(4) << range << dec << setw(0)
> << " " << i << "\n";

That's a nice example. I believe that you have to save and restore the state
of the stream before and after each argument's output, instead of resetting
it to the default one (dec, setw(0), etc.), though.

[snip]
> and BTW the syntax is not so complicated, when you take away
> all of the unused type-specification.
> I began writing a description of format's syntax in Readme.txt
> so that people can have a quick reference specifically written for
> format.

Good! More detailed description of the conceptual differences between
'format' and 'printf' format specifications, and examples like the above one
would be certainly a useful addition to it.

Aleksey


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