gpt4 book ai didi

c++ - 如何将 type 和 mpl::vector 连接到一个新的 vector 中

转载 作者:行者123 更新时间:2023-11-30 04:32:03 25 4
gpt4 key购买 nike

我使用以下构造创建类型的 mpl vector 。

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 vector ,是否可以执行此操作?

最佳答案

我认为 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");
}

如果您希望展平是递归的,您可以在Flatten_impl 中调用Flatten 来展平序列,然后再附加它;但是,请注意此递归仅在您的序列包含包装器而不是直接类型时才有效,例如

struct Struct3
{
typedef mpl::vector< mpl::identity< char >, mpl::identity< double > > type;
}

关于c++ - 如何将 type 和 mpl::vector 连接到一个新的 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7926776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com