Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 合并多个地图_C++_C++14_Boost Hana - Fatal编程技术网

C++ 合并多个地图

C++ 合并多个地图,c++,c++14,boost-hana,C++,C++14,Boost Hana,我已经在boost邮件列表上问过这个问题,但我似乎不太清楚我的意图。也可能是我没有完全理解如何才能做到这一点 我想在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>) ); co

我已经在boost邮件列表上问过这个问题,但我似乎不太清楚我的意图。也可能是我没有完全理解如何才能做到这一点

我想在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 m1=hana::make_map(
hana::make_pair(“键1”\u s,hana::type_c),
hana::make_pair(“键2”\u s,hana::type_c)
);
constexpr auto m2=hana::生成映射(
hana::make_pair(“键3”\u s,hana::type_c),
hana::make_pair(“键4”\u s,hana::type_c),
hana::make_pair(“键5”\u s,hana::type_c)
);
constexpr auto m3=hana::生成映射(
hana::make_pair(“键6”\u s,hana::type_c),
hana::make_pair(“键7”\u s,hana::type_c),
hana::make_pair(“键8”\u s,hana::type_c)
);
我已经得到了两张地图的答案:

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>)
);
constexpr auto result=hana::fold_left(m1,m2,hana::insert);
constexpr auto expected=hana::生成映射(
hana::make_pair(“键1”\u s,hana::type_c),
hana::make_pair(“键2”\u s,hana::type_c),
hana::make_pair(“键3”\u s,hana::type_c),
hana::make_pair(“键4”\u s,hana::type_c),
hana::make_pair(“键5”\u s,hana::type_c)
);
当我查看文档时,我可以看到fold_left接受2或3个参数

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

模板
constexpr自动合并多个映射(映射…参数){
//在这里做一些有用的事情。。。
}
但我不知道如何从这里开始,我仍然没有太多的元编程经验


关于Matthijs,首先,定义
merge2
,如下所示:

auto merge2 = [](auto&& m1, auto&& m2) {
  return hana::fold_left(std::forward<decltype(m1)>(m1),
                         std::forward<decltype(m2)>(m2),
                         hana::insert);
};
我没有测试这个实现,但它应该会告诉您这个想法。如果你不在乎完美的转发,你可以放弃所有的
static_cast
;这只是为了提高运行时效率,以防在地图中存储复制成本高但移动成本低的类型。此外,您将无法在
constexpr
上下文中使用此选项,因为lambda不能出现在常量表达式中。这将在C++17中修复,但现在您可以非常轻松地实现与这些lambda等效的函数对象

[编辑:Hana可能在将来的某个时候实现此
合并
功能。]

[编辑:使用
std::forward
而不是
static\u cast

不应该
std::forward(m1)
工作,用于自我文档?是的,对不起。我习惯于在Hana的实现中使用
static\u cast
,因为它会减少1个函数实例化(这在Hana中会有所不同),但我会编辑我的答案,因为
std::forward
更清晰。您是如何编译示例的?我不断收到
错误:在c++14和GCC 7.3.0上找不到字符串文字运算符“operator”“”,带有“const char[5]”、“long unsigned int”参数hana::make_pair(“key1”\s,hana::type_c)
auto merge2 = [](auto&& m1, auto&& m2) {
  return hana::fold_left(std::forward<decltype(m1)>(m1),
                         std::forward<decltype(m2)>(m2),
                         hana::insert);
};
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
  );
};