简体   繁体   English

如何将 boost::hana::tuple 转换为 std::variant

[英]How to convert a boost::hana::tuple into a std::variant

I have the following code where I want to convert a boost::hana::tuple into a std::variant我有以下代码,我想将boost::hana::tuple转换为std::variant

namespace hana = boost::hana;

template <typename Tuple>
struct to_variant;

template <typename... Ts>
struct to_variant<std::tuple<Ts...>>
{
    using type = std::variant<Ts...>;
};

auto my_tuple = hana::make_tuple(hana::type_c<int>, hana::type_c<char>, hana::type_c<float>);

using my_variant = typename to_variant<my_tuple>::type;

But I always get the error message但我总是收到错误消息

error: type/value mismatch at argument 1 in template parameter list for 'template<class Tuple> struct to_variant'
using my_variant = typename to_variant<my_tuple>::type;

I tried to replace std::tuple with hana::tuple with the same result.我试图用hana::tuple替换std::tuple ,结果相同。

my_tuple is an object, but not type . my_tuple是 object,但不是type (And its type is not std::tuple but boost::hana::tuple which should be used for the specialization.) (并且它的类型不是std::tuple而是boost::hana::tuple应该用于专业化。)

I think you want我想你想要

template <typename Tuple>
struct to_variant;

template <typename... Ts>
struct to_variant<boost::hana::tuple<Ts...>>
//                ^^^^^^^^^^^^^^^^^^
{
    using type = std::variant<Ts...>;
};

auto my_tuple = hana::make_tuple(hana::type_c<int>, hana::type_c<char>, hana::type_c<float>);

using my_variant = typename to_variant<decltype(my_tuple)>::type;
//                                     ^^^^^^^^^        ^

I suggest three corrections in your code (two are already addressed in the other answer):我建议在您的代码中进行三处更正(两个已在另一个答案中解决):

  • Use decltype(my_tuple) to pass a type into to_variant使用decltype(my_tuple)类型传递给to_variant
  • Inside to_variant , use using type = std::variant<typename Ts::type...> to extract the types out of the Hana type_c 's.to_variant中,使用using type = std::variant<typename Ts::type...>从 Hana type_c中提取类型。
  • Use a hana::tuple in the specialization, not a std::tuple .在专业化中使用hana::tuple ,而不是std::tuple More in general, you could also use a template template parameter here.更一般地说,您还可以在此处使用模板模板参数。

Here is the corresponding code:下面是对应的代码:

namespace hana = boost::hana;

template <typename Tuple>
struct to_variant;

template <typename... Ts>
struct to_variant<hana::tuple<Ts...>>
{
    using type = std::variant<typename Ts::type...>;
};

auto my_tuple = hana::make_tuple(hana::type_c<int>, hana::type_c<char>, hana::type_c<float>);

using my_variant = typename to_variant<decltype(my_tuple)>::type;

With Boost.Mp11 , this is pretty short (as always):使用Boost.Mp11 ,这很短(一如既往):

template <typename T> using unwrap = typename T::type;

template <typename T>
using to_variant = mp_rename<mp_transform<unwrap, T>, std::variant>;

Where you would use this as to_variant<decltype(my_tuple)> .您将在哪里使用它作为to_variant<decltype(my_tuple)>

Basically, we first unwrap all the types (this gets us from something like hana::tuple<hana::type_impl<int>::_, hana::type_impl<char>::_, ...> to just hana::tuple<int, char, ...> ) and then we rename the top-level template from hana::tuple to std::variant .基本上,我们首先解开所有类型(这让我们从类似hana::tuple<hana::type_impl<int>::_, hana::type_impl<char>::_, ...>到只是hana::tuple<int, char, ...> ) 然后我们将顶层模板从hana::tuple重命名为std::variant

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

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