【问题标题】:Does std::variant provide functionality similar to boost::variant<>::types?std::variant 是否提供类似于 boost::variant<>::types 的功能?
【发布时间】:2019-09-20 08:58:49
【问题描述】:

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

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

【问题讨论】:

  • 真正的问题是什么?你想做什么?

标签: c++ c++17 boost-variant boost-mpl std-variant


【解决方案1】:

我不是 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

【讨论】:

  • std::tuple -> std::variant 解决这个问题?
  • @R2RT 是的,这似乎是个好主意。 叹息 - 编辑:完成,现在为那个挣扎的大脑喝咖啡。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多