较新的版本
(Boost.Hana > 1.2,Boost >= 1.65)
有boost::hana::union_加入boost::hana::maps的功能。
返回两个地图的并集。
给定两个映射xs和ys,hana::union_(xs, ys)是一个新映射,其中包含 的所有元素xs和 的所有元素ys,没有重复。如果两者xs和ys都包含具有相同键的元素,则采用其中的一个ys。
引用自 Boost.Hana 文档
还有例子:
// Simple 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>)
);
BOOST_HANA_CONSTANT_CHECK(hana::union_(m1, m2) == 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>)
));
// Example with overwriting pair (the same key)
constexpr auto m3 = hana::make_map(
hana::make_pair(hana::type_c<int>, hana::int_c<1>),
hana::make_pair(hana::type_c<bool>, hana::bool_c<true>)
);
constexpr auto m4 = hana::make_map(
hana::make_pair(hana::type_c<char>, hana::char_c<'c'>),
hana::make_pair(hana::type_c<bool>, hana::bool_c<false>)
);
BOOST_HANA_CONSTANT_CHECK(hana::union_(m3, m4) == hana::make_map(
hana::make_pair(hana::type_c<int>, hana::int_c<1>),
hana::make_pair(hana::type_c<bool>, hana::bool_c<false>),
hana::make_pair(hana::type_c<char>, hana::char_c<'c'>)
));
Godbolt在线编译器上的完整工作交互示例。
旧版本
(Boost.Hana <= 1.1,Boost <= 1.64)
基于一些 Boost 论坛主题的修改示例。
constexpr auto map1 = hana::make_map(
hana::make_pair(hana::type_c<TwoOtherKeys1>, hana::type_c<SomeValue>),
hana::make_pair(hana::type_c<SameKey_SameType>, hana::type_c<SomeValue>),
hana::make_pair(hana::type_c<SameKey_DifferType>, hana::type_c<SomeValue>)
);
constexpr auto map2 = hana::make_map(
hana::make_pair(hana::type_c<TwoOtherKeys2>, hana::type_c<SomeValue>),
hana::make_pair(hana::type_c<SameKey_SameType>, hana::type_c<SomeValue>),
hana::make_pair(hana::type_c<SameKey_DifferType>, hana::type_c<OtherValue>)
);
// Creates result sequence by inserting all of (source) `map1` sequence into (initial) `map1` sequence.
// Since both sequences are maps with unique keys, keys that exist in both source and initial map got overwrited.
constexpr auto result = hana::fold(map1, map2, hana::insert);
// Excepted result
constexpr auto expected = hana::make_map(
// Diffrent keys pairs are copied.
hana::make_pair(hana::type_c<TwoOtherKeys1>, hana::type_c<SomeValue>),
hana::make_pair(hana::type_c<TwoOtherKeys2>, hana::type_c<SomeValue>),
// The same key and type pairs are just passed.
hana::make_pair(hana::type_c<SameKey_SameType>, hana::type_c<SomeValue>),
// The same key, differ type - overwrited.
hana::make_pair(hana::type_c<SameKey_DifferType>, hana::type_c<OtherValue>)
);
// Assertion to confirm exceptation
static_assert(result == expected, ""); // OK
Godbolt在线编译器上的完整工作交互示例。
该解决方案与最新版本的 Boost.Hana 中的解决方案完全相同。
全部归功于Louis Dionne的解决方案。