std::variant 是否提供类似于 boost::variant<>::types 的功能?

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

boost::variant 通过 boost::variant<>::types 公开其变体类型列表,可以方便地与 boost::mpl::for_each 一起使用。 std::variant 缺少这样的成员。

我看到 std::variant_alternative 已提供。这可以用来生成 boost::mpl::for_each 可以摄取的类型列表吗?或者它是否启用了不同的迭代策略?

我不是 100% 熟悉 Boost.MPL,但这应该可以满足您的需求:

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