c++c++17boost-variantboost-mplstd-variant

Does std::variant provide functionality similar to boost::variant<>::types?


boost::variant exposes its list of variant types via boost::variant<>::types, which can be conveniently used with boost::mpl::for_each. std::variant is missing such a member.

I see std::variant_alternative is provided. Can this be used to produce a type list that boost::mpl::for_each can ingest? Or does it enable a different iteration strategy?


Solution

  • I'm not 100% familiar with Boost.MPL, but this should do what you're looking for:

    template <class Variant>
    struct mpl_types_impl;
    
    template <class... Ts>
    struct mpl_types_impl<std::variant<Ts...>> {
        using type = boost::mpl::vector<Ts...>;
    };
    
    template <class Variant>
    using mpl_types = typename mpl_types_impl<Variant>::type;
    

    See it live on Wandbox