Boost logo

Boost :

From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2001-12-19 15:11:53


> On Wed, 2001-12-19 at 19:35, Peter Dimov wrote:
> > Choices.html, first bullet argues that boost::format("...") % expr1 % expr2
> > is way cooler than boost::sprintf("...", expr1, expr2).
> >
> > I don't really have a problem with the % syntax (except for the operator
> > precedence, perhaps) but choices.html doesn't do a very good job of
> > explaining its benefits.
>
> I wrote this file quickly before the real documentation.
> I'll try to explain better the benefits of using operator% (rather than
> operator<<)
> It's jsut technical.
> In one word, it's precisely because it has higher precedence than <<.

I'm not sure that that is necessarily a good thing. Consider the
following simple statement, where *op* stands for some currently
unspecfied operator (BTW, I like the general idea of using an operator
here. It makes sense to me).

int arg;
cout << format("String here: %1") *op* arg;

Ideally, I would like to see the following stream calls resulting:

operator<<(cout,"String here: ");
operator<<(cout,arg);

Again ideally, I would rather not see the following sequence:
stringstream(tempstr);
operator<<(tempstr,"String here: ");
operator<<(tempstr,arg);
operator<<(cout,tempstr);

I think that with the choice of %, you're essentially forcing the
implementation to do things with a temporary buffer internally, and
thus copy arguments. That or do some heavy-duty expression templates, I
guess.

Second, I think this choice impacts how a formatter object can be used.
Since I expect the formatter constructor to do some parsing work, I
would like to be able to do something like:

void print_three(ostream &os, int x, int y, int z) {
  static const format("X: %1 Y:%2 Z:%3");
  os << format << x << y << z;
}

Which might be fine for ints in the current setup -- but not if,
instead of an int, I used a recursive data structure whose operator<<
itself called print_three.

IOW, I think I am objecting to the format object carrying the
combination of the basic format string and simultaneously the arguments
being formatted. I ought to be able to have one format object, and
several different objects which reference that format with different
arguments. Maybe I want to bind one of those arguments down, and then
copying that bound object to make a format binding with 1 less arity.

Hmm. This whole thing is starting to seem reminiscent of the bind
library. Can't we express this whole format idea using bind on
operator<< (except that the order of the bindings are specified by a
string instead) ? Maybe we should look back at the bind library for
some ideas.

(I hope that's not too incoherent...)

George Heintzelman
georgeh_at_[hidden]


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