How to obtain standard mpl sequence after fold
Asked Answered
P

1

2

If I use boost::mpl, lets look at the following code:

typedef fold<
  vector<long,float,long>
, set0<>
, insert<_1,_2>
>::type s;

BOOST_MPL_ASSERT_RELATION( size<s>::value, ==, 2 );

How can I turn s into a type t = boost::mpl::set<long,float> again, such that I can use t for selecting a partially specialized template function (on boost::mpl::set``) which extracts the element types and turns it into a std::tuple<long,float> . It is something else

Pluto answered 18/2, 2015 at 14:6 Comment(1)
The design of boost::mpl is a bit archaic since it predates C++11's variadic templates and template aliases. You might have an easier time using something more modern like Eric Niebler's Tiny Meta-Programming Library. E.g., template<class...Ts> using unique_tuple = meta::apply_list<meta::quote<std::tuple>, meta::unique<meta::list<Ts...>>>; will compute std::tuple<long, float> from unique_tuple<long,float,long>.Passade
M
1

Here's a full example. I called the metafunction Unify but obviously you can call it whatever you want.
How it works is quite simple, it simply removes elements from the input sequence one at a time and builds up a variadic list and dumps them into the desired sequence type at the end.

    #include <boost/mpl/set.hpp>
    #include <boost/mpl/front.hpp>
    #include <boost/mpl/size.hpp>
    #include <boost/mpl/insert.hpp>
    #include <boost/mpl/erase_key.hpp>
    #include <tuple>

    template <template <class...> class OutSeqType,
              class Sequence,
              std::size_t nSeqSize,
              class ... Elements>
    struct Unify
    {
        typedef typename boost::mpl::front<Sequence>::type Next;
        typedef typename Unify<
            OutSeqType,
            typename boost::mpl::erase_key<Sequence, Next>::type,
            nSeqSize - 1, Next, Elements...>::type type;
    };

    template <template <class...> class OutSeqType,
              class Sequence,
              class ... Elements>
    struct Unify<OutSeqType, Sequence, 0ul, Elements...>
    {
        typedef OutSeqType<Elements...> type;
    };

    int main()
    {
        typedef boost::mpl::insert<
            boost::mpl::insert<
                boost::mpl::insert<
                    boost::mpl::set<>,
                    int>::type,
                float>::type,
            int*>::type Set;

        typedef Unify<
            std::tuple,
            Set,
            boost::mpl::size<Set>::type::value
            >::type Set2;

        //This compile error will print the type of Set2
        Set2::asdfl;
    }

For some reason I'm not sure about I couldn't use pop_front on a set so I used erase_key instead. This restricts it to associative containers, but it could be generalized to any type of container with a little more work. I'll leave that as an exercise.

Melia answered 18/2, 2015 at 15:10 Comment(3)
cool thanks, so this snipped can unify anythin coming out of a mpl::transform or mpl::fold etc.?Pluto
@Pluto I believe in the mpl the idea is that you don't care what the actual type is, as long as it behaves the way you want. That said, I just don't know for sure, I've never actually found such functionality, instead sticking with the "don't-care-what-it-is-just-do-what-I-want" ideology.Melia
@Pluto It can unify anything, including things that are already unified. You can also unify into a container with different semantics than the source one, like how I moved from a set to a tuple for example.Melia

© 2022 - 2024 — McMap. All rights reserved.