有没有办法获得Boost Variant的类型向量(扩展递归)

时间:2016-02-04 15:24:29

标签: c++ boost boost-variant

Boost Variant类的成员类型为 types 。它是模板参数中使用的类型的Boost MPL序列。在递归的情况下,例如:

typename boost::make_recursive_variant<int, std::vector<boost::recursive_variant_>>::type;

将创建一个序列(类型成员):

boost::mpl::l_item<boost::mpl::long_<2>,int,boost::mpl::l_item<boost::mpl::long_<1>,std::vector<boost::variant<boost::detail::variant::recursive_flag<int>,std::vector<boost::recursive_variant_,std::allocator<boost::recursive_variant_> >,boost::detail::variant::void_,boost::detail::variant::void_,...........> >,boost::mpl::l_end> >

正如您所看到的,std::vector<boost::recursive_variant_,std::allocator<boost::recursive_variant_> >尚未替换为原始变体类型。

我发现一些内部实用程序正在执行此转换,但它没有文档,很难遵循到期的宏。

现在我可以用MPL做到但它可能与Variant实现不匹配(有一些递归限制等)。

有没有办法让这个类型的序列转换为原始的Variant实用程序?

[编辑]

我的目标是获得:

boost::mpl::vector<int, std::vector<MyVariantType, std::allocator<MyVariantType>>

[编辑]

这就是我此刻递归替换的方式。我的最终目标是编写一个方便的Boost.Variant包装器来添加类型接口(完成实现但不满意使用我自己的实现进行递归类型替换)。 (更多https://github.com/FictionIO/WrappedVariant

template<typename PlaceholderT, typename WithT>
struct substitute_placeholder
{
    template<typename InputT>
    struct apply
    {
        template<typename T>
        struct substitute
        {
            using type  = T;
        };

        template<template <typename, typename ...> typename T, typename ParamT, typename ... ParamsT>
        struct substitute<T<ParamT, ParamsT...>>
        {
            template<typename ... TypesT>
            struct packed_types{};

            template<typename Type1T, typename Type2T>
            struct pack_types;

            template<typename TypeT, typename ... TypesT>
            struct pack_types<packed_types<TypesT...>, TypeT>
            {
                using type  = packed_types<TypesT..., TypeT>;
            };

            template<typename ... TypesT>
            struct unpack
            {
                using type  = void;
            };

            template<typename ... TypesT>
            struct unpack<packed_types<TypesT...>>
            {
                using type  = T<TypesT...>;
            };

            using originals         = boost::mpl::vector<ParamT, ParamsT...>;
            using func              = typename substitute_placeholder<PlaceholderT, WithT>:: template apply<boost::mpl::_1>;
            using substitutedTypes  = typename boost::mpl::transform<originals, func>::type;
            using packed            = typename boost::mpl::fold<substitutedTypes, packed_types<>, pack_types<boost::mpl::_1, boost::mpl::_2>>::type;
            using type              = typename unpack<packed>::type;
        };

        template<>
        struct substitute<PlaceholderT>
        {
            using type  = WithT;
        };

        using type      = typename substitute<InputT>::type;
    };
};

template<typename ImplT, typename TypesVecT>
struct to_substitude_types_vec
{
    using func      = typename substitute_placeholder<boost::recursive_variant_, ImplT>:: template apply<boost::mpl::_1>;
    using type      = typename boost::mpl::transform<TypesVecT, func>::type;
};

using originalTypes     = typename boost::mpl::copy<VariantT::types, boost::mpl::back_inserter<boost::mpl::vector<>>>::type;
using substitutedTypes  = typename to_substitude_types_vec<VariantT, originalTypes>::type

0 个答案:

没有答案