如何用任意类型的元函数转换 hana::tuple_t?
How to tranform a hana::tuple_t with an arbitrary type meta-function?
假设我有两种类型,每种类型都有一个内部类型:
struct A1{
using type = int;
};
struct B1{
using type = double;
};
我从我的 classes(例如 auto Types = hana::tuple_t<A1, B1>
)创建了一个 hana::tuple_t
,我想得到我的 [=] 内部类型的类似 hana::tuple_t
30=](例如 hana::tuple_t<A1::type, B1::type>
)
我想使用转换函数得到结果 tuple_t:
auto result = hana::transform(Types, [](auto t){return t::type;});
我收到编译错误:
error: ‘t’ is not a class, namespace, or enumeration
使用 hana::tranform
实现此目的的正确方法是什么?
您可以使用hana::template_
template<typename O>
using inner_type = typename O::type;
auto result = hana::transform(Types, hana::template_<inner_type>);
tuple_t
生成 tuple
,它不能包含 类型 。相反,它包含 hana::type
,它们是 值 , 代表 类型。 template_
将类型级函数(template
类型别名或 class)转换为值级 "metafunction"。如果你想为 transform
使用显式 lambda,你可以,但它变得很血腥:
auto result = hana::transform(Types, [](auto t) { return hana::type_c<typename decltype(t)::type::type>; })
lambda 中 t
的类型对于某些 T
是 hana::type<T>
,因此 decltype(t)
从值中解包该类型,第一个 ::type
获取 T
,第二个 ::type
获取您的目标类型,type_c
将其全部包装起来。
假设我有两种类型,每种类型都有一个内部类型:
struct A1{
using type = int;
};
struct B1{
using type = double;
};
我从我的 classes(例如 auto Types = hana::tuple_t<A1, B1>
)创建了一个 hana::tuple_t
,我想得到我的 [=] 内部类型的类似 hana::tuple_t
30=](例如 hana::tuple_t<A1::type, B1::type>
)
我想使用转换函数得到结果 tuple_t:
auto result = hana::transform(Types, [](auto t){return t::type;});
我收到编译错误:
error: ‘t’ is not a class, namespace, or enumeration
使用 hana::tranform
实现此目的的正确方法是什么?
您可以使用hana::template_
template<typename O>
using inner_type = typename O::type;
auto result = hana::transform(Types, hana::template_<inner_type>);
tuple_t
生成 tuple
,它不能包含 类型 。相反,它包含 hana::type
,它们是 值 , 代表 类型。 template_
将类型级函数(template
类型别名或 class)转换为值级 "metafunction"。如果你想为 transform
使用显式 lambda,你可以,但它变得很血腥:
auto result = hana::transform(Types, [](auto t) { return hana::type_c<typename decltype(t)::type::type>; })
lambda 中 t
的类型对于某些 T
是 hana::type<T>
,因此 decltype(t)
从值中解包该类型,第一个 ::type
获取 T
,第二个 ::type
获取您的目标类型,type_c
将其全部包装起来。