Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-07-19 16:47:39


AMDG

Joel FALCOU wrote:
> I started porting over some old codes that used hand-made
> MPL<->concrete object code to Fusion.
> I'm currently trying to have the following to work with no results so
> far :

Strange as it may seem, fusion::transform is not
the right tool in this case, mpl::transform is what you
want. fusion::transform is lazy. It returns a transform_view.
mpl::transform works because fusion sequences /are/ mpl
sequences. The default behavior of mpl::transform
is to preserve the input sequence type. i.e. transforming
an mpl::vector gives an mpl::vector, transforming an mpl::list
gives an mpl::list, and transforming a fusion::vector gives
a fusion::vector.

#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/type_traits/add_const.hpp>
#include <boost/type_traits/function_traits.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/back_inserter.hpp>

namespace bf = boost::fusion;
namespace mpl = boost::mpl;

struct make_arg {
    template<class T> struct apply {
        typedef typename boost::add_reference<
            typename boost::add_const<T>::type
>::type type;
    };
};

template<class Args> struct tree {
    typedef Args children;
    typedef typename mpl::transform<Args, make_arg>::type contents;

    template<class T0,class T1> tree( const T0& t0 , const T1& t1 )
      : mArgs(t0, t1) {}

    template<size_t I>
    typename bf::result_of::at_c<contents,I>::type get() const {
        return bf::at_c<I>(mArgs);
    }
    contents mArgs;
};

int main() {
    int k = 3;
    float u = 7.2f;
    tree< bf::vector<int, float> > tree(k, u);
}

In Christ,
Steven Watanabe


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