Boost logo

Boost Users :

From: Joel de Guzman (joel_at_[hidden])
Date: 2005-09-21 18:26:04


Alan M. Carroll wrote:
> The previous message reminded me of something I wanted to ask.
>
> I wrote an extension for Boost.Tuple that, given a type T and a Boost
> N-tuple, computes the N+1 tuple (this turns out to be handy for my
> application). The basic definition looks like this:
>
> template < typename R, typename T, int N = 0 >
> struct add_type {
> typedef typename add_type< typename R, typename T, length<
> typename T>::value >::type type;
> };
>
> There's a bunch of specializations that handle the actual implementation
> that are obvious specializations for non-zero values of N.
>
> It's used like this:
>
> typedef typename add_type<interval, typename super::region>::type region
>
> where "interval" is the type we want to prepend in to the tuple and
> "super::region" is the existing tuple. So if super::region is tuple<A,B>
> then region is tuple<interval, A, B>.
>
> Now, the actual style question: Should I have made the add_type be
> (stylistically) a Boost.MPL metafunction instead? E.g., a metafunction
> of two arguments (type, tuple) that produces the result tuple? Is that
> the common practice? Or is that only done when intending to use
> Boost.MPL? That would mean tthe usage (regardless of whether Boost.MPL
> itself was in use) would be
>
> typedef typename add_type_mf::apply<interval, super::region>::type region;
>
> Is that preferred (as in, a little more wordy but much clearer because
> of the convention)?

You need both a function and a metafuncion. In fusion, you
have this functions:

     push_back(tup, x);
     push_front(tup, x);

and its corresponding metafunction:

     meta::push_back<tup_type, x_type>::type
     meta::push_front<tup_type, x_type>::type

If you haven't heard of fusion, here's an excerpt from
the docs that I am writing right now:

'''
Fusion is an implementation of Boost.Tuple library by Jaakko
Jarvi with more sequence types (vectors, lists, sets, maps)
iterators, views and algorithms (for_each, transform, etc.).
The structure is somewhat modeled after MPL. It is named "fusion"
because the library is the "fusion" of compile time
metaprogramming with runtime programming.
'''

It's (98%) feature complete now. All the important sructures
are in place. I'll be asking for a formal boost review very
soon now.

Cheers,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net