Boost logo

Boost :

From: James Kanze (kanze_at_[hidden])
Date: 2002-02-20 15:57:33


Karl Nelson <kenelson_at_[hidden]> writes:

    [...]
|> > The obvious use of format is as a temporary, as we've seen in most
|> > of the examples. But one could argue for other possibilities:

|> > format( "%s %d" ) fmt ;
|> > for ( int i = 0 ; i < 10 ; ++ i ) {
|> > cout << fmt.with( s[ i ] ).with( x[ i ] ) << '\n' ;
|> > }

|> > In this case, the conversion would cause the format object to revert to
|> > the parameter insertion state. But that would forbid:

|> > format( "%s %d" ) fmt ;
|> > fmt.with( s ).with( i ) ;
|> > std::cout << fmt << '\n' ;
|> > logStream << fmt << '\n' ;

|> Okay I handled this part very differently. format was the function
|> to do the .with() and call a rewind.

|> So
|> format_expr fmt("%s %d");
|> fmt(s)(i);
|> std::cout << fmt << '\n' ;
|> logStream << fmt << '\n' ;

|> works because fmt still has the arguments.

If I remember right, that's the way mine works too. But I'm not
convinced it is the only, or even the best, way. Just throwing out
ideas.

|> But this wont unless you add a rewind statment or use format()

|> format_expr fmt( "%s %d" );
|> for ( int i = 0 ; i < 10 ; ++ i ) {
|> cout << fmt( s[ i ] )( x[ i ] ) << '\n' ; // too many arguments.
|> }

|> This will
|> format_expr fmt( "%s %d" );
|> for ( int i = 0 ; i < 10 ; ++ i ) {
|> cout << format (fmt, s[ i ], x[ i ] ) << '\n' ;
|> }

|> Alternatively...
|> format_expr fmt( "%s %d" );
|> for ( int i = 0 ; i < 10 ; ++ i ) {
|> cout << fmt( s[ i ]) ( x[ i ] ) << '\n' ;
|> fmt.rewind();
|> }

That's an idea. I vaguely had in mind that if the format had actually
been generated, the rewind would be implicit on the next parameter, but
I'm not sure that such implicit state changes are a good idea.

-- 
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