合并多个地图

时间:2016-08-25 14:22:47

标签: c++ c++14 boost-hana

我已经在加强邮件列表上问了这个问题,但我似乎并没有明白我的意图。也许我不能完全理解如何实现这一目标。

我想在hana中合并多个地图,请参阅以下代码示例:

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 m3 = hana::make_map(
    hana::make_pair("key6"_s, hana::type_c<std::string>),
    hana::make_pair("key7"_s, hana::type_c<std::string>),
    hana::make_pair("key8"_s, hana::type_c<std::string>)
);

我已经得到了如何为两张地图做到这一点的答案:

constexpr auto result = hana::fold_left(m1, m2, hana::insert);
constexpr auto expected = 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>)
);

当我查看文档时,我可以看到fold_left需要2或3个参数。

看起来我需要这样的东西: fold_left(fold_left(m1,m3,hana :: insert),m2,hana :: insert);

template<typename Map...>
constexpr auto merge_multiple_maps(Map... args) {
  // do something useful here...
}

但我不确定如何从这里开始,我仍然没有多少元编程经验...

问候,Matthijs

1 个答案:

答案 0 :(得分:3)

首先,按如下方式定义merge2

auto merge2 = [](auto&& m1, auto&& m2) {
  return hana::fold_left(std::forward<decltype(m1)>(m1),
                         std::forward<decltype(m2)>(m2),
                         hana::insert);
};

然后,将merge定义为merge2

的递归应用
auto merge = [](auto&& m1, auto&& ...ms) {
  return hana::fold_left(
    hana::make_basic_tuple(std::forward<decltype(ms)>(ms)...),
    std::forward<decltype(m1)>(m1),
    merge2
  );
};

我没有测试这个实现,但它应该给你这个想法。如果您不关心完美转发,可以放弃所有static_cast;这只是为了提高运行时效率,以防您在地图中存储昂贵的复制但价格便宜的类型。此外,您无法在constexpr上下文中使用它,因为lambdas不能出现在常量表达式中。这将在C ++ 17中修复,但是现在你可以很容易地实现与这些lambdas等效的函数对象。

[编辑:Hana可能会在将来某个时候实施此merge功能。] [修改:使用std::forward代替static_cast。]