如何加入两个或多个助推融合贴图

How do I join two or more boost fusion maps?

本文关键字:融合 两个 何加入      更新时间:2023-10-16

我想从两种boost::fusion::map类型创建一个关联序列。其中一个映射中包含的类型可能存在于另一个映射,如果是这种情况,我只想在结果序列中得到一个具有该键的类型。也就是说,我希望在加入后是唯一的。

传统的联接操作似乎允许重复键,所以它似乎不是一个解决方案。有人知道我是怎么做到的吗?

// Here is what I've got:
using namespace boost::fusion;
map<
  pair<int, int>,
  pair<double, int>> Map1;
map<
  pair<bool, int>,
  pair<double, int>> Map2;
// I want to join Map1 with Map2 such that I have
static_assert(std::is_same<Map3, map<
  pair<int, int>,
  pair<double, int>,
  pair<bool, int>>>::value, "");

您可能需要手动消除欺骗:在完整的c++14装备中在Coliru上直播

auto r = 
    as_map(
        fold(
            fold(m1, m2, [](auto accum, auto elem) { return erase_key<typename decltype(elem)::first_type>(accum); }),
            m1, 
            [](auto accum, auto elem) { return insert(accum, boost::fusion::end(accum), elem); }
        )); 

这太古怪了。如果你用函子代替lambdas,你会得到类似的结果:

auto r = 
    as_map(
        fold(
            fold(m1, m2, erase_corresponding()), 
            m1, 
            insert_helper()
        ));

一个简单的实现LiveOnColiru仍然依赖于初步的c++1y支持:

 struct erase_corresponding {
    template<typename T, typename U> 
        auto operator()(T map, U elem) const {
            return boost::fusion::erase_key<typename U::first_type>(map);
        }
};
struct insert_helper {
    template<typename T, typename U> 
        auto operator()(T map, U elem) const {
            return boost::fusion::insert(map, boost::fusion::end(map), elem);
        }
};

然而,要使其完全符合c++03,您需要使用RESULT_OF(我留给读者练习)

来拼写它