Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-09-12 13:55:30


David Abrahams wrote:
>"Reece Dunn" <msclrhd_at_[hidden]> writes:
>
> >>I'm generally not a great fan of statefulness, but this also seems
> >>like a reasonable thing to want:
> >>
> >> namespace io = boost::io;
> >> std::cout << io::sequence_delimiters("{ ", " }");
> >> std::cout << io::sequence( vec );
> >> // output: { 1, 2, 3 }
> >
> > You can achieve something like this using:
> >
> > io::formatter< const char * > fmt( "{ ", " }" );
> > std::cout << io::sequence( vec ).format( fmt );
> >
> > If there are any better alternatives, I am willing to listen.
>
>Well, I think my alternative is better if you want a stateful change,
>since yours doesn't accomplish that. My suggestion is certainly
>consistent with normal io manipulators. The point is to stream a
>bunch of sequences without having to repeat the format part.

I understand what you are saying and yes your version is neater. However,
implementing something like this would have performance penalties (because
you will need to create/store the preset values at run-time).

It will also change the way all sequence constructs are rendered. (n-ary
types have a different rendreing so won't be affected). As I understand,
this is the desired effect. But then:
[1] how do you select sequence or n-ary formatting?
[2] what about extending this to incorporate - for example - tree or string
formatting?
[3] how do you extend this to formatter types that have additional
delimeters?
[4] how do you know when to extract the default delimeter values from the
stream?

Adding a formatter as a parameter to io::sequence, etc. would further
complicate function lookup. Perhapse if I had an is_format_object< T
>::value trait I could use MPL wizardry to distinguish between format
object and delimeter formatter, allowing:

    io::formatter< const char * > fmt( "{ ", " }" );
    std::cout << io::sequence( vec, fmt ); // output: { a, b, c }

The problem with this is: what if you want to specify a format object and a
delimeter formatter? This might work, but the implementation becomes more
complex.

Another idea would be to overload an operator like:

    std::cout << io::sequence( vec )( fmt ); // output: { a, b, c }

or

    std::cout << io::sequence( vec )[ fmt ]; // output: { a, b, c }

but this would further add to the confusion. If operator() was adopted, it
would be possible to have that as a shorthand for format(). For example:

    std::cout << io::sequence( vec )( " | " ); // output: [ 1 | 2 | 3 ]

Regards,
Reece

_________________________________________________________________
Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo


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