将闭包作为参数传递给构造函数c ++

编程入门 行业动态 更新时间:2024-10-24 02:39:06
本文介绍了将闭包作为参数传递给构造函数c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在下面的代码中,我想摆脱奇怪的构造函数采取各种类型的函数指针,参数列表,必须保存(以及所有成员变量,需要保存所有这些),而是使用一个闭包来做所有的事情,留下事件与一个成员变量,像闭包; 。有没有办法使用C ++ 0x中的closures?

In the code below, I'd like to get rid of the weird constructors taking various types of function pointers, and the parameter lists that have to be saved off (along with all the member variables that are required to hold all this), and instead use a closure to do all of that, leaving Event with a single member variable that is something like Closure closure;. Is there a way to do this using closures in C++0x?

#include <iostream> #include <list> #include <functional> #include <algorithm> class Event { std::function<double(double, double)> func1; bool func1Defined; double double1, double2; std::function<double(int, int, int)> func2; bool func2Defined; int int1, int2, int3; public: void setFalse() { func1Defined = false; func2Defined = false; } Event(std::function<double(double, double)> func, double double1, double double2) { setFalse(); func1 = func; func1Defined = true; this->double1 = double1; this->double2 = double2; } Event(std::function<double(int, int, int)> func, int int1, int int2, int int3) { setFalse(); func2 = func; func2Defined = true; this->int1 = int1; this->int2 = int2; this->int3 = int3; } void run() { double ret = 0; if (func1Defined) ret = func1(double1, double2); else if (func2Defined) ret = func2(int1, int2, int3); /* do something with ret */ std::cout << ret << "\n"; } }; double twoDoubleParam(double a, double b) { return a + b; } double threeIntParam(int a, int b, int c) { return (a + b) / c; } int main(int argc, char* argv[]) { std::list<Event> events; events.push_back(Event(*twoDoubleParam, 1.0, 3.5)); events.push_back(Event(*threeIntParam, 2, 4, 2)); std::for_each(events.begin(), events.end(), [&](Event event) { event.run(); }); int y; std::cin >> y; return 0; }

不推荐,但如果boost是最好的/只是简单的方法

Prefer no boost, but if boost is best/only easy way to do this I'd like to know how.

推荐答案

看起来你的事件应该只在第一个位置保存一个 std :: function< double()> :独立使用lambda或者,你想绑定参数创建一个nullary函数(就我所知,如果每个调用都有实际的参数,你显然会在 std :: function< Signature> 中使用相应的签名。

It seems your Event should just hold a std::function<double()> in the first place: independent on using lambda or not, you want to bind the arguments to create a nullary function (as far as I can tell; if there are actual parameters to each call, you'd obviously use a corresponding signature in std::function<Signature>.

假设您事件有 fun $ c> std :: function< double()> 和一个构造函数如

Assuming you Event has a member fun of type std::function<double()> and a constructor like

template <typename Fun> Event::Event(Fun fun): fun(fun) {}

你可以,例如使用

events.emplace_back([](){ return twoDoubleParam(1.0, 3.5); }); events.emplace_back(std::bind(&threeIntParam, 2, 4, 2));

两种方法都可以使用 std :: bind()或λ。我只是混合使用得到每一个。显然,如果你使用lambda和参数不是常量而是值,你可以使用 [=](){...} 来捕获闭包值值。

Both approaches could use std::bind() or lambda. I just mixed the use to get one of each. Obviously, if you use the lambda and the parameters are not constants but rather values, you'd use [=](){ ... } to capture the closure values by value.

更多推荐

将闭包作为参数传递给构造函数c ++

本文发布于:2023-07-29 08:59:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数   将闭包

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!