Boost logo

Boost :

From: Samuel Krempp (krempp_at_[hidden])
Date: 2001-12-19 15:03:14


On Wed, 2001-12-19 at 19:35, Karl Nelson wrote:

> Why use the operator "%" for combining items. It is not streamlike
> and introduces precidence problems which C++ coders do not normally
> have with streams. Previous discussions (when I submitted my formatting
> class) here concluded operator << was a better choice.

Your code used a "endf" marker, to stop the format::operato<<
and return to stream's.

eg, the following would be a problem :
cout << format(" %1 %2") << x << y << "\n";
// poor example, the \n would be happier inside the format-string.
// but well..

Because, accepting this, eg by making the format class act as a simple
stream-wrapper when all the arguments have been fed, means you won't
detect arity errors if the user forgets an argument.

The alternative you chose, using an "endf" marker, brings robustness vs
users error.
But it's ugly.

On the other hand, with operator% :
1. the code is smaller than with '<<' and, in my opinion, nicer.
2. arity problems are detected, even when using format objects in the
middle of long stream outputs chains.

----
3. you have to guard against "% 1+2", and uses parenthesis around + and
- operators, while it is not needed for stream's <<.
point 3 is an inconvenient, but I think it's not a big price.
If you forget to print (1+2), it means :
(  format("%1 %2") % 1 ) + 2 % <the_rest_of_the_line>
So it tries to call operator+ on a format object, => the error is
detected at compile time.
regards,
-- 
Samuel

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