代码之家  ›  专栏  ›  技术社区  ›  motam79

如何使用任意类型的元函数转换hana::tuple\t?

  •  0
  • motam79  · 技术社区  · 7 年前

    struct A1{
        using type = int;
    };
    struct B1{
        using type = double;
    };
    

    我创造了一个 hana::tuple_t 从我的课堂(例如。 auto Types = hana::tuple_t<A1, B1> )我想买一个类似的 hana::元组 hana::tuple_t<A1::type, B1::type> )

    我想使用transform函数来获得结果元组:

    auto result = hana::transform(Types, [](auto t){return t::type;});
    

    我得到编译错误:

    error: ‘t’ is not a class, namespace, or enumeration
    

    使用以下哪种方法实现此目的 hana::tranform ?

    1 回复  |  直到 7 年前
        1
  •  2
  •   HTNW    7 年前

    你可以用 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 s、 哪些是 那个 代表 template_ 打开类型级别函数(a template 在值级别“元函数”中键入别名或类)。如果要为 transform ,你可以,但它会变得血淋淋:

    auto result = hana::transform(Types, [](auto t) { return hana::type_c<typename decltype(t)::type::type>; })
    

    t 在lambda是 hana::type<T> 对一些人来说 T decltype(t) 从第一个值展开该类型 ::type 得到 ,第二 ::类型 获取您的目标类型,以及 type_c 把它全部包起来。

    推荐文章