Boost logo

Boost :

From: Dave Abrahams (abrahams_at_[hidden])
Date: 2000-03-06 06:59:43


I'm starting this thread to provoken discussion of potential improved
interfaces for formatted output (and possibly input). I hope this will
eventually result in a boost library.

I'm actually having a hard time convincing a colleague to avoid printf (and
related functions), and he has a point:

1. printf-style output statements are often easier to read:
    printf("please type %d rows of %d characters\n", rows, cols);
vs.
    std::cout << "please type " << rows << " rows of " << cols
        << " characters" << std::endl;

2. it's easy for him to remember printf formatting statements.

3. C++-style sticky formatting (e.g. once you say ""<<std::hex" you have to
undo it to get back to decimal) is usually NOT what you want.
    std::cout << std::hex << (unsigned)address;
        ...
    std::cout << length; // oops, this was supposed to be decimal!

4. It's really easy to forget spaces in all the right places with iostreams,
and costs many characters to put them in.

Of course, most of the downsides of printf are well-known:
A. Not type-safe
B. Easy to crash it
C. Hard to extend (you need va_list, not-even-standard-yet vsnprintf(), &c)

There is also the not-so-well-known disadvantage that:
D. it can theoretically cause bigger executables than using a well-optimized
C++ i/o library, since once you use printf for anything, ALL formatting code
must be linked in.

Format strings have some potential advantages for internationalization. It's
not uncommon to want to output a series of values in a different order for a
new language. Something that let you write this could be extremely useful:

    boost::format( "%1 chickens can be found in coop number %2")
         % chicken_cnt % coop_num;

Thus, for a translation to French, simply substitute the format string:

    "Dans la cage le numéro %2 on y trouve %1 poulets"

which is read from a file or other resource.

[the use of operator%() stolen from Python - I'm open to discussion about
this choice]

Python has a print statement which just puts in spaces for you. So the
incantation "print x, y, z" prints x, y, and z to the console separated by
spaces (followed by a newline if there is no trailing comma). In the absence
of format strings, wouldn't it be easier to write something like:

std::cout << x, y, z;

which is much better than either of these below:

std::cout << "%1 %2 %3" % x % y %z;
std::cout << x << " " << y << " " << z;

I'm not sure I see a good way to accomplish this, though :(. Is it illegal
or just a very bad idea (or neither?) to write this in the global namespace:

    template <class T>
    ostream& operator,(ostream& s, const T& x) { s << " " << x; }

For formatting specifiers, I propose something like this:

std::cout << boost::hex(x) << " " << boost::precision(5.3, y);

P.S. Also, can't we do something about std::endl? This seems like way too
many characters to type just to produce a newline!

-Dave


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