Is there a way to obtain the map type?

喜你入骨 提交于 2020-01-03 03:36:07

问题


I have the following code:

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)
);

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>, ...>; 

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.


回答1:


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

  1. You could simply wrap the same expression in 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)
    ));
    
  2. 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>;
    


来源:https://stackoverflow.com/questions/36185951/is-there-a-way-to-obtain-the-map-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!