在boost::hana中给定一个键元组,如何从映射中获取值元组
How to get tuple of values out of map given a tuple of keys in boost::hana?
我知道我可以使用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,了解将原始函数提升到对象和其他整洁的无点编程的工具。
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- 重载元组索引运算符-C++
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将fold表达式与std::一起用于两个元组
- std::ranges::elements_view,用于自定义类似元组的数据
- 在boost::hana中给定一个键元组,如何从映射中获取值元组
- 编译按映射类型获取元素的映射错误元组
- std::元组到元组的映射和使用 emplace
- 带有映射的 Lambda 函数,其中键是元组,值是双精度
- 整数和元组的静态 STL 映射返回 0
- C++映射中搜索元组错误:无法将"int"左值绑定到"int&&"
- c++存储映射元组的引用,以便在另一个函数中使用
- 复制地图<双精度,元组<双精度,双精度>>映射<双精度,双精度>没有循环?
- 如何实现可变元组映射操作
- 使用C++元组类型作为映射中的键
- 如何在Boost中使用元组映射
- 为不使用元组的自定义映射模拟std::map迭代器
- 将C++重载函数映射到异构元组上
- 在C++中,在不损失速度的情况下提高元组映射的内存消耗