2014-09-29 81 views
3

我想从两个boost::fusion::map类型创建一个associative sequence。其中一个映射中包含的类型可能存在于另一个映射中,并且如果是这种情况,我只想在结果序列中使用具有该键的单一类型。也就是说,我希望加入后是唯一的。如何加入两个或更多增强融合地图?

传统的join操作似乎允许重复的键,所以它似乎不是一个解决方案。有谁知道我怎么能做到这一点?

// 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, ""); 

回答

3

你可能必须手动消除受骗者:完全C++ 14轮Live On 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); } 
     )); 

这是时髦的。如果您使用仿函数的lambda表达式,而不是取代它,你最终会类似于:

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

一个简单的实现Live On Coliru初步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(我离开作为一个练习读者)

+0

刚刚意识到第二次可以简化成一个简单的连接:) – sehe 2014-09-30 12:41:21