[英]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):我建议在您的代码中进行三处更正(两个已在另一个答案中解决):
decltype(my_tuple)
to pass a type into to_variant
decltype(my_tuple)
将类型传递给to_variant
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
中提取类型。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.