2014-09-29 10 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); } 
     )); 

에. 대신 람다의 펑로 교체하면 유사한 끝장 :

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

Live On Coliru 여전히 1Y 지원 ++ 예비 C에 기댈 간단한 구현 :에,

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