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

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

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

关于c++ - std::variant 是否提供类似于 boost::variant<>::types 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58025118/

相关文章:

C++ 库 : Questions need Definite Answers(Opinions)

C++:if 内部循环的性能影响

c++ - 播放wav文件后,是否需要删除缓冲区?

c++ - 如何比模板参数解析更喜欢隐式转换?

c++ - 如何概括具有变体/访问者的树结构

c++ - std::map 中的线程安全

c++ - 如何让 `static_assert` 打印失败时得到的值?

c++ - 有条件地在类中创建成员

c++ - 使输出流运算符为 boost::variant<std::vector<int>, int, double> 工作的正确方法是什么

C++ 相互递归变体类型(再次)