Search code examples
c++boostboost-hana

Is there a way to obtain the map type?


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.


Solution

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