如何在折叠后获得标准mpl序列 [英] How to obtain standard mpl sequence after fold

查看:196
本文介绍了如何在折叠后获得标准mpl序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用boost :: mpl,请看下面的代码:

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 );

如何将 s t = boost :: mpl :: set< long,float> ,这样我可以使用 t 一个部分专用的模板函数(在boost :: mpl :: set``),它提取元素类型并将其转换为 std :: tuple< long,float> 。它是别的东西

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

推荐答案

这里有一个完整的例子。我调用元函数 Unify ,但显然你可以调用它任何你想要的。

它的工作原理很简单,它只是从输入序列中删除元素

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;
    }

由于某种原因,我不确定我不能使用 pop_front 设置,所以我使用 erase_key 。这将它限制到关联容器,但它可以推广到任何类型的具有更多工作的容器。我会把它作为一个练习。

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.

这篇关于如何在折叠后获得标准mpl序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆