Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-06-29 21:06:25


----- Original Message -----
From: "Samuel Krempp" <krempp_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, June 29, 2001 9:06 PM
Subject: Re: [boost] Review : format2, yet another 'printf class'

>
>
> The short form of positional arguments is now implemented, as "%N "
> That is, you need to put a space after the number, that gets eaten.
> so
> format("%1 bla") % BLA gives : BLAbla
> This extra space is necessary to support both syntaxes.

I don't like it. I don't know which "both" syntaxes are, but I'd vote for
one syntax and no spaces.

> The conversion to string is also here (although conversion operators are
> rarely appreciated, I think it is quite safe here)
>
> Sample usage now (v1.4) :
>
> string s = format(" %5 . %5$=6s . le format %5 , c'%3 %1 %2 .\n")
> %"le" %"bonheur" %"est" %"trop" * setfill('_') %"bref";
>
> cerr << s;
> assert( s == " bref. _bref_ . le format bref, c'est le bonheur.\n");

Some alternatives to implicit conversion:

namespace boost {
    std::string str(const format&);
}

std::string s = str(format(" %5 . %5$=6s . le format %5 , c'%3 %1 %2
.\n")
       %"le" %"bonheur" %"est" %"trop" * setfill('_') %"bref");

OR:

std::string s = (format(" %5 . %5$=6s . le format %5 , c'%3 %1 %2 .\n")
       %"le" %"bonheur" %"est" %"trop" * setfill('_') %"bref").str();

> > > . if you want good passing of arguments (ie not copy by value, but
> > > passing T& / const T&),
> > > you need to define 2^10 functions.. (!)
> > >
> >
> > You must know something I don't. With the version I've been using, all
> > arguments are passed to the constructor as const T&, and I've only got 9
> > constructors. Why do you need the non-const variations?
>
> that's the point, as long as you dont need a non-const version, fine.
> But it sounds strange to impose this restriction
> "only objects with const operator<< defined are formattable".
> (even if needing to modify the object at print-time sounds like
> bad-design..)

Yes it does! In general you are right about the 2^n explosion. I think this
case might be an exception.

> Since boost aims at very general libraries, I think this was a cause for
> the halt on this approach of boost::format.
> At least it was one of the issues raised at the last format thread.
>
> else, the function approach has some possibilities that can't be beaten by
> passing arguments through an operator !

I do like the operator approach, though.

-Dave


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