在boost::hana中给定一个键元组,如何从映射中获取值元组

How to get tuple of values out of map given a tuple of keys in boost::hana?

本文关键字:元组 映射 获取 键元 hana boost 一个      更新时间:2023-10-16

我知道我可以使用operator[]boost::hana::map获得单个值。但我发现"向量化"非常困难:从多个键的元组中获取多个值。

假设我们有一个hana::mapthe_map:

#define BOOST_HANA_CONFIG_ENABLE_STRING_UDL
#include <boost/hana/string.hpp>
#include <boost/hana/map.hpp>
#include <string>
namespace hana = boost::hana;
using namespace hana::literals;
auto the_map = hana::make_map(
hana::make_pair("key1"_s, std::string("value1")),
hana::make_pair("key2"_s, int(42)),
hana::make_pair("key3"_s, std::string("value3")),
hana::make_pair("key4"_s, float(3.14)),
hana::make_pair("key5"_s, std::string("value5"))
);

最后,我想将这个映射的值解压到一个函数调用中。因为C++是按位置(而不是按名称(传递参数的,所以我需要获得一个具有特定顺序的值元组。hana::map不保证条目的顺序,所以我需要通过键来提取它们,有效地实现了命名元组方向的一些东西,以实现类似于通过名称传递函数参数的东西。

因此,假设我有一个函数myfun和一个编译时密钥列表:

#include <boost/hana/tuple.hpp>
void myfun(std::string par1, int par2, std::string par3, float par4, std::string par5) {
//...
}
constexpr auto the_keys = hana::make_tuple("key1"_s, "key2"_s, "key3"_s, "key4"_s, "key5"_s);

我想要的是在给定键the_keys的情况下,以某种方式从the_map中获得一个值元组,这样我就可以用int main() {hana::unpack(the_items, myfun);}调用函数。

到目前为止,我做得最好的是以下失败的C++代码:

#include <boost/hana/at_key.hpp>
#include <boost/hana/fold_left.hpp>
template<typename T, typename U>
auto accessor(U sequence, T key) {
static_assert(hana::Foldable<U>::value);
return hana::insert(sequence, 0_c, ms[key]);
}
auto the_items_manually=accessor(accessor(accessor(hana::make_tuple(), "key1"_s), "key2"_s), "key3"_s); //this works
auto the_items = hana::fold_left(keys, hana::make_tuple(), accessor); //compile error

最后一行产生错误。

error: no match for call to ‘(const boost::hana::fold_left_t) (const boost::hana::tuple<boost::hana::string<'k', 'e', 'y', '1'>, boost::hana::string<'k', 'e', 'y', '2'>, boost::hana::string<'k', 'e', 'y', '3'>, boost::hana::string<'k', 'e', 'y', '4'>, boost::hana::string<'k', 'e', 'y', '5'> >&, boost::hana::tuple<>, <unresolved overloaded function type>)’
auto the_items = hana::fold_left(keys, hana::make_tuple(), accessor);

我的GCC 7.3.0 C++14编译器也注意到,

boost/hana/fold_left.hpp:26:30: note: candidate: template<class Xs, class State, class F> constexpr decltype(auto) boost::hana::fold_left_t::operator()(Xs&&, State&&, F&&) const
constexpr decltype(auto) fold_left_t::operator()(Xs&& xs, State&& state, F&& f) const {
^~~~~~~~~~~

boost/hana/fold_left.hpp:26:30: note:   template argument deduction/substitution failed:
/home/Adama-docs/Adam/MyDocs/praca/IMGW/repos/all5/eulag-verification/simple_test/test5.cpp:67:72: note:   couldn't deduce template parameter ‘F’
auto the_items = hana::fold_left(keys, hana::make_tuple(), accessor);
  ^

我知道,这个错误希望我明确地键入一个适当类型的东西作为模板参数。好吧,不管是什么,Hana可用性的重点是不必键入类型,所以我认为我需要有一个不同的方法。有人能帮我吗?

这里给hana::fold_left的函数accessor必须是lambda或函数对象,否则我认为它会起作用。(不支持函数指针(

为了获得更好的编译时性能,hana::unpack在可能的情况下比hana::fold_left更好,因为它不必为列表中的每个元素创建一个中间对象。

下面是一个使用高阶函数捕获地图的示例:

#define BOOST_HANA_CONFIG_ENABLE_STRING_UDL
#include <boost/hana.hpp>
#include <string>
#include <iostream>
namespace hana = boost::hana;
using namespace hana::literals;
int main() {
auto get_values = [](auto const& map) {
return [&](auto const& ...key) {
return hana::make_tuple(map[key]...);
};
};
auto myfun = [](std::string par1, int par2, std::string par3, float par4, std::string par5) {
std::cout << par1 << 'n'
<< par2 << 'n'
<< par3 << 'n'
<< par4 << 'n'
<< par5 << 'n'
;
};
auto the_map = hana::make_map(
hana::make_pair("key1"_s, std::string("value1")),
hana::make_pair("key2"_s, int(42)),
hana::make_pair("key3"_s, std::string("value3")),
hana::make_pair("key4"_s, float(3.14)),
hana::make_pair("key5"_s, std::string("value5"))
);
constexpr auto the_keys = hana::make_tuple("key1"_s, "key2"_s, "key3"_s, "key4"_s, "key5"_s);
auto values = hana::unpack(the_keys, get_values(the_map));
hana::unpack(values, myfun);
}

您可能还想查看Boost.Hof,了解将原始函数提升到对象和其他整洁的无点编程的工具。