1

我使用以下构造来创建类型的 mpl 向量。

struct Struct1{
    typedef int type;
};
struct Struct2{
    typedef float type;
};
template<typename T> struct TypeReader{
    typedef typename T::type type;
};

int main()
{
    typedef bm::vector<Struct1,Struct2> MPLVector;
    typedef bm::transform<MPLVector, TypeReader<bm::_1>>::type TypeList;
    static_assert(bm::equal<TypeList, bm::vector<int,float> >::value, "Is not same");
}

到目前为止,这按预期工作。现在我想做的是以下

struct Struct3{
    typedef bm::vector<char,double> type;
};
typedef bm::vector<Struct1,Struct2,Struct3> MPLVector;
typedef bm::transform<MPLVector, TypeReader<bm::_1>>::type TypeList;
static_assert(bm::equal<TypeList, bm::vector<int,float,char,double> >::value, "Is not same");

这不起作用。那么我需要如何更改我的 MetaFunction 结构,它可以同时与 typedef 和 mpl::vector 一起使用?
或者,如果这不可能,如果我将所有类型 typedef 更改为 mpl 向量,是否可以这样做?

4

2 回答 2

2

我认为这是不可能的mpl::transform,因为它需要从源序列中的单个元素生成结果序列中的多个元素。但是,这可以通过mpl::fold以及专业化来完成mpl::is_sequence

// Default case
template < typename Seq, typename TWrapper, typename Enable = void >
struct Flatten_imp
    : mpl::push_back< Seq, typename TWrapper::type >
{
};

// Sequence case
template < typename Seq, typename TWrapper >
struct Flatten_imp< 
    Seq,
    TWrapper, typename
    boost::enable_if< 
        mpl::is_sequence< typename 
            TWrapper::type 
        > 
    >::type
>   
{
    typedef mpl::joint_view< 
        Seq, typename 
        TWrapper::type 
    > type;
};

template < typename Seq >
struct Flatten
    : mpl::fold< Seq, mpl::vector<>, Flatten_imp< mpl::_, mpl::_ > >
{}

int main()
{
    typedef mpl::vector< Struct1, Struct2, Struct3 > MPLVector;
    typedef Flatten< MPLVector >::type TypeList;

    static_assert(
        mpl::equal<
            TypeList, 
            mpl::vector< int, float, char, double > 
        >::value, "Is not same");
}

如果您希望展平是递归的,您可以在附加之前调用FlattenFlatten_impl平序列;但是,请注意此递归仅在您的序列包含包装器而不是直接类型时才有效,例如

struct Struct3
{
    typedef mpl::vector< mpl::identity< char >, mpl::identity< double > > type;
}
于 2011-10-28T09:43:14.703 回答
0

您必须专门TypeReader针对参数是序列(mpl 向量、融合列表/向量/元组)的情况;

mpl::back_inserter在这种情况下,结合with的力量mpl::copy(从参数的序列复制到目标类型)

我目前没有时间演示这个。有关如何专门研究序列概念的提示,您可以查看boost::enable_if. 但是,只需专注于template <typename> mpl::vector

于 2011-10-28T08:31:13.410 回答