Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-07-17 19:37:24


AMDG

Istvan Buki wrote:
> Dear Boost experts,
>
> I'm trying to copy data between different types of fusion sequences
> with limited success.
> The example below shows what I'm trying to achieve.
>
> <snip>
> Does fusion provide a way to achieve what I'm trying to do?

The elements of the map are fusion pair. You need to
extract the second element.

#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/map.hpp>
#include <boost/fusion/include/pair.hpp>
#include <boost/fusion/include/transform.hpp>

namespace fields {
    struct i;
    struct l;
    struct d;
}

template<class F>
struct result_of_second;

template<class Self, class Arg1>
struct result_of_second<Self(Arg1)> {
    typedef const typename Arg1::second_type& type;
};
template<class Self, class Arg1>
struct result_of_second<Self(Arg1&)> {
    typedef typename Arg1::second_type& type;
};
template<class Self, class Arg1>
struct result_of_second<Self(const Arg1&)> {
    typedef const typename Arg1::second_type& type;
};

struct second {
    template<class F>
    struct result {
        typedef typename result_of_second<F>::type type;
    };
    template<class T>
    typename T::second_type& operator()(T& t) const { return(t.second); }
    template<class T>
    const typename T::second_type& operator()(const T& t) const {
return(t.second); }
};

int main() {
    using namespace boost::fusion ;

    typedef map<
        pair<fields::i, int>,
        pair<fields::l, long>,
        pair<fields::d, double>
> map_type;

    typedef vector<int, long, double> vect_t;

    vect_t tup1(1, 2, 3.0);
    map_type m = tup1;
    vect_t tup2 = transform(m, second());

    // expected result: tup1 == tup2
}

> As a side question, I was wondering whether it is possible to
> synthetize the vect_t type from map_type (with MPL?)

Indeed it is possible,

typedef result_of::as_vector<
    mpl::transform<
        map_type,
        result_of::second<mpl::_1>,
        mpl::back_inserter<mpl::vector0<> >
>::type
>::type vect_t;

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