c – 使用lambda参数数组
发布时间:2020-12-16 09:51:12 所属栏目:百科 来源:网络整理
导读:我正在玩C 14 lambdas(一般来说只是lambdas)我有一个函数(管道)我正在尝试写.前提是它需要一个单位lambda和一组一元lambda然后它将在单元上运行并产生一个新单元发送到管道中的下一个单元,直到你通过最后一个lambda并返回最后一个单元.我目前的代码是: auto
我正在玩C 14 lambdas(一般来说只是lambdas)我有一个函数(管道)我正在尝试写.前提是它需要一个单位lambda和一组一元lambda然后它将在单元上运行并产生一个新单元发送到管道中的下一个单元,直到你通过最后一个lambda并返回最后一个单元.我目前的代码是:
auto pipeline = [](auto u,auto callbacks[]){ for(int i = 0; i<sizeof(callbacks)/sizeof(callbacks[0]);i++){ u = bind(u,callbacks[i]); } return u; }; 目前的问题是,clang正在回击阵列说: testFuture.cpp:183:111: error: no matching function for call to object of type 'func::<lambda at ./func.hpp:30:19>' cout<<"pipeline(unit(10),{addf(4),curry(mul,2)}):"<<bind(unit(bind(unit(10))(addf(4))))(curry(mul,2))<<"|"<<pipeline(unit(10),{{addf(4),2)}})()<<endl; ^~~~~~~~ ./func.hpp:30:19: note: candidate template ignored: couldn't infer template argument '$auto-0-1' auto pipeline = [](auto u,auto callbacks[]){ ^ 1 error generated. 这是不可能与lambdas?我需要甩掉std :: function吗?我只是以错误的方式解决这个问题吗? 解决方法
退后一步,你试图在一系列值上执行(左)折叠.因为在C中每个闭包都有一个唯一的类型,你想要在元组上进行折叠,而不是数组.作为额外的好处,我们将更加通用并接受具有任何返回类型的仿函数,如果例如我们不能我们使用std :: function来模拟箭头类型.
#include <utility> // std::forward,std::integer_sequence #include <type_traits> // std::remove_reference_t #include <tuple> // tuple protocol,e.g. std::get,std::tuple_size namespace detail { template<typename Functor,typename Zero,typename Tuple,typename Int> Zero foldl(Functor&&,Zero&& zero,Tuple&&,std::integer_sequence<Int>) { return std::forward<Zero>(zero); } template<typename Functor,typename Int,Int Index,Int... Indices> decltype(auto) foldl(Functor&& functor,Tuple&& tuple,std::integer_sequence<Int,Index,Indices...>) { return detail::foldl( functor,functor(std::forward<Zero>(zero),std::get<Index>(std::forward<Tuple>(tuple))),std::forward<Tuple>(tuple),Indices...> {}); } } // detail template<typename Functor,typename Tuple> decltype(auto) foldl(Functor&& functor,Tuple&& tuple) { return detail::foldl( std::forward<Functor>(functor),std::forward<Zero>(zero),std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>() ); } 你没有告诉我们什么绑定应该实现,但here’s an example涉及功能的反向组合. (包括integer_sequence和朋友的有限实现,因为它们似乎在Coliru上不可用 – 类型特征也缺少别名.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |