Boost logo

Boost Users :

Subject: Re: [Boost-users] Tuples: Can someone help me generalize this?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-09-17 11:39:18


AMDG

Derrick Hathaway wrote:
> Thank you, Sir, for your quick response. I just got around to testing
> it, and I've run into a snag. a fusion::vector is larger than the sum
> of it's parts, and I would like something that is not. In other words:
>
> sizeof(vector<float, float>) != sizeof(float) + sizeof(float)

Please create a trac ticket for this. It looks like EBCO is not working
properly,
because the compiler refuses to allocate two instances of
boost::fusion::sequence_root
at the same address.

> A tuple seems to fit, as I described earlier, but I'm not sure how to
> generalize this. I've thought that there might be a way to generalize
> a tuple to provide an interface like my_tuple<N, Type> using the cons.
> Any thoughts?

Unfortunately cons doesn't quite work because it it doesn't have the
constructor you want.

You can use the general mpl::inserter to create a specialization of
tuple.

#include <boost/mpl/transform.hpp>
#include <boost/mpl/always.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/inserter.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>

#include <iostream>

template<class Tuple, class T>
struct tuple_push_front;

template<BOOST_PP_ENUM_PARAMS(9, class T), class T>
struct tuple_push_front<boost::tuple<BOOST_PP_ENUM_PARAMS(9, T)>, T> {
    typedef boost::tuple<T, BOOST_PP_ENUM_PARAMS(9, T)> type;
};

// create a sequence of 5 elements
typedef boost::mpl::range_c<int, 0, 5> range;
// convert all the elements to float and make the result a fusion vector.
typedef boost::mpl::transform<range, boost::mpl::always<float>,
    boost::mpl::inserter<
        boost::tuple<>,
        tuple_push_front<boost::mpl::_1, boost::mpl::_2>
>
>::type tuple_type;

int main() {
   tuple_type t(0, 1, 2, 3, 4);
   std::cout << typeid(tuple_type).name() << std::endl;
}

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