【问题标题】:How to join two Boost Hana maps?如何加入两个 Boost Hana 地图?
【发布时间】:2019-02-13 02:28:34
【问题描述】:

我有两个boost::hana::maps 要加入。

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

【问题讨论】:

    标签: c++ boost metaprogramming boost-hana


    【解决方案1】:

    较新的版本

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

    boost::hana::union_功能加入boost::hana::maps。

    返回两个映射的并集。

    给定两个映射xsyshana::union_(xs, ys) 是一个包含xs 的所有元素和ys 的所有元素的新映射,没有重复。 如果xsys 都包含具有相同键的元素,则采用ys 中的元素。

    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

    基于来自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的解决方案的所有功劳。

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      相关资源
      最近更新 更多