简体   繁体   English

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

[英]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 . boost::variant通过boost::variant<>::types公开其变体类型列表,可以方便地与boost::mpl::for_each一起使用。 std::variant is missing such a member. std::variant缺少这样的成员。

I see std::variant_alternative is provided.我看到提供了std::variant_alternative Can this be used to produce a type list that boost::mpl::for_each can ingest?这可以用来生成boost::mpl::for_each可以摄取的类型列表吗? Or does it enable a different iteration strategy?或者它是否支持不同的迭代策略?

I'm not 100% familiar with Boost.MPL, but this should do what you're looking for:我对 Boost.MPL 不是 100% 熟悉,但这应该可以满足您的需求:

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在 Wandbox 上现场观看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM