Boost logo

Boost :

From: James Kanze (kanze_at_[hidden])
Date: 2002-01-27 12:11:15


"mfdylan" <dylan_at_[hidden]> writes:

|> As for the formatting options (width/precision specifications and so
|> forth), I really can't see a lot of point of encouraging continued
|> use of the cryptic printf-style, which can hardly make translators'
|> jobs easier.

This is partially true. The printf-style isn't particularly transparent
for non-programmers. (For that, we need something like the old Basic's
print using, or Cobol picture clauses.) But it is essential that at
least some of the formatting information (e.g. width and precision)
appear in the format string, and it is essential for numeric values that
the basic type (floating or integral) also be specified. And I"ve not
seen that in any of the alternative proposals.

    [...]
|> I'd prefer

|> format("array[%1] = %2", i, array[i]);

The cleanest is obviously something like:

    format( "array[%1] = %2" ).with( i ).with( array[ i ] );

The grouping is obvious (and the compiler's grouping matches up with
what the programmer expects), and since the operation doesn't really
correspond to anything for which there is a pre-defined operator (except
<<).

|> Although if you allow for manipulators between parameters, even with
|> only 9 parameters you would have to support a helluva lot of
|> template variants.

Manipulators, as such, are a hack, which aren't needed. What can be
useful is passing additional parameters to with (above), when the width
or the precision are calculated dynamically.

|> And I guess it's handy to be able to insert parameters one- by-one
|> if necessary. To be honest I never really understood the argument
|> against using '<<' - I assume the only reason was that the statement

|> cout << format("array[%1] = %2") << i << array[i] << ".\n";

|> Could either be an error (inserting too many parameters), or an
|> intended insertion of ".\n" into the stream after the formatted
|> message. I probably opt on the safe side and say it's error and
|> require extra parentheses:

This is basically the route I took for my format class. It allows
either operator<<, or the function with. It handles formatting
naturally, a la X/Open printf. For users who insist on <<, it also
supports the * size specification for width and precision, although
IMHO, "with" with additional parameters is the preferrable route.

-- 
James Kanze                                mailto:kanze_at_[hidden]
Conseils en informatique orientée objet/
                    Beratung in objektorientierter Datenverarbeitung
Ziegelhüttenweg 17a, 60598 Frankfurt, Germany Tel. +49(0)179 2607481

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