Merge multiple cards

I already asked about this on the mailing list, but I was not so clear because of my intentions. It may also be I do not quite understand how I can do this.

I want to combine several cards in hana, see the following code example:

constexpr auto m1 = hana::make_map(
    hana::make_pair("key1"_s, hana::type_c<std::string>),
    hana::make_pair("key2"_s, hana::type_c<std::string>)
);

constexpr auto m2 = hana::make_map(
    hana::make_pair("key3"_s, hana::type_c<std::string>),
    hana::make_pair("key4"_s, hana::type_c<std::string>),
    hana::make_pair("key5"_s, hana::type_c<std::string>)
);

constexpr auto m3 = hana::make_map(
    hana::make_pair("key6"_s, hana::type_c<std::string>),
    hana::make_pair("key7"_s, hana::type_c<std::string>),
    hana::make_pair("key8"_s, hana::type_c<std::string>)
);

I already got an answer on how to do this for two cards:

constexpr auto result = hana::fold_left(m1, m2, hana::insert);
constexpr auto expected = hana::make_map(
    hana::make_pair("key1"_s, hana::type_c<std::string>),
    hana::make_pair("key2"_s, hana::type_c<std::string>),
    hana::make_pair("key3"_s, hana::type_c<std::string>),
    hana::make_pair("key4"_s, hana::type_c<std::string>),
    hana::make_pair("key5"_s, hana::type_c<std::string>)
);

When I checked the documentation, I see that fold_left takes 2 or 3 arguments.

Looks like I need something like: fold_left (fold_left (m1, m3, hana :: insert), m2, hana :: insert);

template<typename Map...>
constexpr auto merge_multiple_maps(Map... args) {
  // do something useful here...
}

But I'm not sure how to proceed from here, and I still do not have much experience in the metaprogram ...

Regards, Matthijs

+4
source share
1 answer

First define merge2as follows:

auto merge2 = [](auto&& m1, auto&& m2) {
  return hana::fold_left(std::forward<decltype(m1)>(m1),
                         std::forward<decltype(m2)>(m2),
                         hana::insert);
};

merge merge2:

auto merge = [](auto&& m1, auto&& ...ms) {
  return hana::fold_left(
    hana::make_basic_tuple(std::forward<decltype(ms)>(ms)...),
    std::forward<decltype(m1)>(m1),
    merge2
  );
};

, . , static_cast; , , , , . , constexpr, lambdas . ++ 17, , .

[: Hana merge - .] [: std::forward static_cast.]

+3

Source: https://habr.com/ru/post/1652485/


All Articles