c++ - 如何连接两个 Boost Hana map ?

标签 c++ boost metaprogramming boost-hana

我有两个 boost::hana::map 要连接在一起。

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

如何获取包含两者的 map ,如下所示?

constexpr auto result = 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>) 
); 

最佳答案

更新版本

(Boost.Hana > 1.2,Boost >= 1.65)

boost::hana::union_函数用于连接boost::hana::map

Returns the union of two maps.

Given two maps xs and ys, hana::union_(xs, ys) is a new map containing all the elements of xs and all the elements of ys, without duplicates. If both xs and ys contain an element with the same key, the one in ys is taken.

Cite from Boost.Hana docs

还有例子:

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

完整的交互示例 here on Godbolt在线编译器。


旧版本

(Boost.Hana <= 1.1,Boost <= 1.64)

基于 some Boost forums topic 中的修改示例.

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

完整的交互示例 here on Godbolt在线编译器。

该解决方案与最新版本的 Boost.Hana 中的解决方案完全相同。


Louis Dionne 解决方案的所有功劳.

关于c++ - 如何连接两个 Boost Hana map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54661568/

相关文章:

c++ - 编译cmake代码时获取 "ld: symbol(s) not found for architecture x86_64"

c++ - boost 源代码

python - 如何从类中的方法动态创建模块级函数

apache-flex - 在 groovy/grails 中使用元编程拦截服务方法调用

c# - 我可以使用 Roslyn 进行编译时代码重写吗?

c++ - 相关名称的参数相关查找

c++ - 重载 '-' 运算符

java - 链表实现的先有鸡还是先有蛋的困境

c++ - Arduino控制6个不同模式的电磁阀

c++ - 如何在 Windows 上停止 boost::ioservice