Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++ 在boost::hana中,给定一个键元组,如何从映射中获取值元组?_C++_Boost_Metaprogramming_Boost Hana - Fatal编程技术网

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

C++ 在boost::hana中,给定一个键元组,如何从映射中获取值元组?,c++,boost,metaprogramming,boost-hana,C++,Boost,Metaprogramming,Boost Hana,我知道我可以使用操作符[]从boost::hana::map获取单个值。但我发现“向量化它”非常困难:从多个键的元组中获取多个值 假设我们有一个hana::map映射: #define BOOST_HANA_CONFIG_ENABLE_STRING_UDL #include <boost/hana/string.hpp> #include <boost/hana/map.hpp> #include <string> namespace hana = boost

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

假设我们有一个hana::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"))
);
我想从给定键的
映射中得到一个值元组,这样我就可以用
int main(){hana::unpack(the_items,myfun);}
调用函数

<> P>我至今所做的最好的是下面的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

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

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

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

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

\define BOOST\u HANA\u CONFIG\u ENABLE\u STRING\u UDL
#包括
#包括
#包括
名称空间hana=boost::hana;
使用名称空间hana::文本;
int main(){
自动获取_值=[](自动常量和映射){
返回[&](自动常量&…键){
返回hana::make_tuple(映射[键]…);
};
};
自动myfun=[](标准::字符串par1,整数par2,标准::字符串par3,浮点par4,标准::字符串par5){

STD::这是一个很棒的解决方案!谢谢你的优化提示。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);
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);
                                                                        ^