Search code examples
c++dictionaryboostsetboost-hana

boost hana: create a map from a set and a default value


I have a boost::hana::set of types and want to create a map with it, where the values are bool's.

// I have a hana set:
auto my_set = hana::make_set(hana::type_c< int >, hana::type_c< float > ...);

// and want to transform it to a map with a given runtime value as values:
auto wanted_map = hana::make_map(
    hana::make_pair(hana::type_c< int >, false),
    hana::make_pair(hana::type_c< float >, false),
    ...
);

Solution

  • hana::set is hana::Foldable so you can use hana::unpack. Consider this example:

    #include <boost/hana.hpp>
    
    namespace hana = boost::hana;
    
    
    int main() {
      constexpr auto make_pair_with = hana::curry<2>(hana::flip(hana::make_pair));
    
      auto result = hana::unpack(
        hana::make_set(hana::type_c< int >, hana::type_c< float >),
        hana::make_map ^hana::on^ make_pair_with(false)
      );
    
      auto expected = hana::make_map(
        hana::make_pair(hana::type_c< int >, false),
        hana::make_pair(hana::type_c< float >, false)
      );
    
      BOOST_HANA_RUNTIME_ASSERT(result == expected);
    }