从增强融合适应结构中获取成员类型列表
Getting a list of member types from a boost fusion adapted struct
我有像这样的 boost fusion 适应结构:
struct A {
int x;
double y;
std::string z;
};
BOOST_FUSION_ADAPT_STRUCT(
A,
x,
y,
z
)
我想在编译时迭代改编的类型。例如。如果我有一个 class 包装类型:
template <typename T> class Foo { ... };
然后我希望能够在给定我的结构 A 的情况下获得类型 std::tuple<Foo<int>, Foo<double>, Foo<std::string>>
。我在这里使用 std::tuple
作为示例;它可以是另一个可变类型模板 class.
欢迎提供 c++17 解决方案。
将自适应融合结构转换为类似 std::tuple
:
的帮助程序
template<class Adapted, template<class ...> class Tuple = std::tuple>
struct AdaptedToTupleImpl
{
using Size = boost::fusion::result_of::size<Adapted>;
template<size_t ...Indices>
static Tuple<typename boost::fusion::result_of::value_at_c<Adapted, Indices>::type...>
Helper(std::index_sequence<Indices...>);
using type = decltype(Helper(std::make_index_sequence<Size::value>()));
};
template<class Adapted, template<class ...> class Tuple = std::tuple>
using AdaptedToTuple = typename AdaptedToTupleImpl<Adapted, Tuple>::type;
验证:
using AsTuple = AdaptedToTuple<A>;
static_assert(std::is_same_v<std::tuple<int, double, std::string>, AsTuple>);
将元函数应用于元组中每种类型的助手:
template<class List, template<class> class Func> struct ForEachImpl;
template<class ...Types, template<class ...> class List, template<class> class Func>
struct ForEachImpl<List<Types...>, Func>
{
using type = List<Func<Types>...>;
};
template<class List, template<class> class Func>
using ForEach = typename ForEachImpl<List, Func>::type;
验证:
static_assert(std::is_same_v<ForEach<AsTuple, std::add_pointer_t>, std::tuple<int*, double*, std::string*>>);
另请查看 Boost.MP11
库。它具有 mp_transform
元功能,相当于上述 ForEach
功能。
我有像这样的 boost fusion 适应结构:
struct A {
int x;
double y;
std::string z;
};
BOOST_FUSION_ADAPT_STRUCT(
A,
x,
y,
z
)
我想在编译时迭代改编的类型。例如。如果我有一个 class 包装类型:
template <typename T> class Foo { ... };
然后我希望能够在给定我的结构 A 的情况下获得类型 std::tuple<Foo<int>, Foo<double>, Foo<std::string>>
。我在这里使用 std::tuple
作为示例;它可以是另一个可变类型模板 class.
欢迎提供 c++17 解决方案。
将自适应融合结构转换为类似 std::tuple
:
template<class Adapted, template<class ...> class Tuple = std::tuple>
struct AdaptedToTupleImpl
{
using Size = boost::fusion::result_of::size<Adapted>;
template<size_t ...Indices>
static Tuple<typename boost::fusion::result_of::value_at_c<Adapted, Indices>::type...>
Helper(std::index_sequence<Indices...>);
using type = decltype(Helper(std::make_index_sequence<Size::value>()));
};
template<class Adapted, template<class ...> class Tuple = std::tuple>
using AdaptedToTuple = typename AdaptedToTupleImpl<Adapted, Tuple>::type;
验证:
using AsTuple = AdaptedToTuple<A>;
static_assert(std::is_same_v<std::tuple<int, double, std::string>, AsTuple>);
将元函数应用于元组中每种类型的助手:
template<class List, template<class> class Func> struct ForEachImpl;
template<class ...Types, template<class ...> class List, template<class> class Func>
struct ForEachImpl<List<Types...>, Func>
{
using type = List<Func<Types>...>;
};
template<class List, template<class> class Func>
using ForEach = typename ForEachImpl<List, Func>::type;
验证:
static_assert(std::is_same_v<ForEach<AsTuple, std::add_pointer_t>, std::tuple<int*, double*, std::string*>>);
另请查看 Boost.MP11
库。它具有 mp_transform
元功能,相当于上述 ForEach
功能。