[tuple][mpl][fusion] How to "prune" or "collapse" a tuple?

Hi all,
[added this line to make gmane happy.]
I'm looking for a way to remove "empty" values from a tuple. In other words, say I have a tuple T: tuple<E, int, E, double, E, char, E> T; // where E can be either null_type, or mpl::void_, etc. I'd like for it to collapse into: tuple<int, double, char> T2; Now, the reason I say "values" is because I found a way to do just that, but only for the types, which you'll find below. However, I can't figure out how to then *construct* the collapsed tuple given the original tuple (and its values.) Specifically, it's the `???' below that I'm missing. Thanks! // Collapse code: template <typename Sequence> struct tuple_from_sequence : mpl::reverse_fold < Sequence , tuples::null_type , tuples::cons<mpl::_2, mpl::_1> > {}; template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9> struct collapse <tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> > { typedef tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> original; typedef mpl::list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> sequence; typedef typename mpl::remove<sequence, tuples::null_type>::type removed; typedef typename tuple_from_sequence<removed>::type type; static type construct(original const& t) { // return ???; } };

AMDG AJG wrote:
// Collapse code:
template <typename Sequence> struct tuple_from_sequence : mpl::reverse_fold < Sequence , tuples::null_type , tuples::cons<mpl::_2, mpl::_1> > {};
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9> struct collapse <tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> > { typedef tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> original; typedef mpl::list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> sequence;
typedef typename mpl::remove<sequence, tuples::null_type>::type removed; typedef typename tuple_from_sequence<removed>::type type;
static type construct(original const& t) { // return ???; } };
Try using fusion::remove http://www.boost.org/libs/fusion/doc/html/fusion/algorithm/transformation/fu... In Christ, Steven Watanabe

Steven Watanabe <watanabesj <at> gmail.com> writes:
Try using fusion::remove http://www.boost.org/libs/fusion/doc/html/fusion /algorithm/transformation/functions/remove.html
Thanks! That function seems to do exactly what I need. However, is it expected to work using boost::tuples? (I tried using fusion::vectors and it works perfectly.) The issue I'm running into is that remove() returns a filter_view, which, I'm guessing, is convertible to a fusion::vector but not to a boost::tuple. Is there any way to make that latter conversion work? I've already included: #include <boost/fusion/include/remove.hpp> #include <boost/fusion/adapted/boost_tuple.hpp> But it looks like boost::tuple isn't being adapted fully. Thanks again.

AJG schrieb:
Steven Watanabe <watanabesj <at> gmail.com> writes:
Try using fusion::remove http://www.boost.org/libs/fusion/doc/html/fusion /algorithm/transformation/functions/remove.html
Thanks! That function seems to do exactly what I need. However, is it expected to work using boost::tuples? (I tried using fusion::vectors and it works perfectly.)
The issue I'm running into is that remove() returns a filter_view, which, I'm guessing, is convertible to a fusion::vector but not to a boost::tuple. Is there any way to make that latter conversion work?
I've already included:
#include <boost/fusion/include/remove.hpp> #include <boost/fusion/adapted/boost_tuple.hpp>
But it looks like boost::tuple isn't being adapted fully.
Including /fusion/adapted/boost_tuple.hpp just transforms any boost::tuple into an implementation of the random access sequence concept http://www.boost.org/doc/libs/1_43_0/libs/fusion/doc/html/fusion/sequence/co... i.e. all boost::tuple instantiations go along with fusion's intrinsic sequence functions. To construct a tuple from a fusion sequence, you should stick to the fusion container. For example: #include <boost/fusion/container/vector.hpp> #include <boost/fusion/algorithm/transformation/remove.hpp> struct E{}; int main() { using namespace boost::fusion; typedef vector<E, int, E, double, E, char, E> vec; typedef result_of::as_vector< result_of::remove<vec, E>::type >::type fvec; fvec f(0, 1.0, 'c'); } -Christopher
participants (3)
-
AJG
-
Christopher Schmidt
-
Steven Watanabe