帮助将适应的融合结构转换为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 函数。