有没有办法获取地图类型? [英] Is there a way to obtain the map type?

查看:116
本文介绍了有没有办法获取地图类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

auto myMap = hana::make_map(
    hana::make_pair(hana::type_c<int>, 2),
    hana::make_pair(hana::type_c<char const*>, "hi"),
    hana::make_pair(hana::type_c<double>, 3.0)
);

是否可以预先知道"myMap"的类型?我尝试使用:

Is there a way to know the type of 'myMap' beforehand? I try it with:

using MyMap = hana::map<hana::pair<hana::type<int>, int>, ...>; 

但是失败,因为decltype(myMap)是hana :: map<实现定义的>.有没有一种'result_of'元函数可以给出imp定义的类型?像:

but it fails because decltype(myMap) is hana::map< implementation-defined >. Is there a kind of 'result_of' metafunction that would give the imp-defined type? Like:

using MyMap = typename hana::result_of_map<hana::pair<hana::type<int>, int>, ...>::type;

我需要用于存储类成员映射的类型.

I need the type to store a class member map.

推荐答案

如果您确实需要预先使用此类型,则可以使用以下两种解决方法:

If you really need the type beforehand here are two possible solutions:

  1. 您只需将相同的表达式包装在decltype中.

using MyMap = decltype(hana::make_map(
    hana::make_pair(hana::type_c<int>, 2),
    hana::make_pair(hana::type_c<char const*>, "hi"),
    hana::make_pair(hana::type_c<double>, 3.0)
));

  • 对于使用与键相同类型的用例,可以制作一个简单的类型别名模板.

  • For your use case of using the same type as the key, you could make a simple type alias template.

    template <typename ...T>
    using type_map_t = decltype(hana::make_map(hana::make_pair(hana::type_c<T>, std::declval<T>())...));
    
    using MyMap = type_map_t<int, char const*, double>;
    

  • 这篇关于有没有办法获取地图类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆