Boost logo

Boost :

From: Hamish Mackenzie (boost_at_[hidden])
Date: 2001-07-01 22:33:14


One other thing I forgot to ask about. Are there any plans for an append
operator? Or how about operator << and operator >>?

I think this might solve the problem John mentioned without a language
extension.

In my type vector class I included an operator | which I used to build
instantiated type vector template instances.
        f( tv0() | 1 | float( 2 ) | 3 )
(BTW is there a different term I can use to refer to template instanciation
to distinguish it from class instanciation?)

I would envisage it working in the tuple class like this
        f( tuple<>() | 1 | float( 2 ) | 3 );
Which would be the same as
        f( tuple< int, float, int >( 1, 2, 3 ) );

operator, might be a better operator (though it scares me because I can never
remember the evaluation order).
        f( tuple<>(), 1, 2, 3 );
I prefer | over , as what it actually does is append to the tuple and , makes
tuple<>() look like the first element not the empty tuple.

Another option is + but it is more commonly used than | and would make
        f( tuple<>() | 1 | 1+1 | 3 );
into
        f( tuple<>() + 1 + (1+1) + 3 );

My current favorite though is operator << (after all it is already used for a
similar thing in streams)
        f( tuple<>() << 1 << 1+1 << 3 );

And operator >> could go with it
        template< class T >
        f( T t )
        {
                int a;
                float b;
                int c;
                t >> a >> b >> c;
        }
If you are cunning the same code that persists an object to a stream could be
used to store it in an appropriate tuple!

Also
typdef tuple<> t0;
might help make things more readable

        f( t0() << 1 << 2 << 3 );

Yet another variation would be to have a function
template< class T >
tuple< T > t( T t ) { return tuple< T >( t ); }

Then use operator() as append
        f( t( 1 )( 2 )( 3 ) )

But I still like << and >> best.

Hamish Mackenzie


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