使用 boost::function 作为函数参数?
我想调用函数 foo 并让它表现不同。
(我采用策略或命令模式。它们看起来和我很相似。)
我的总体计划如下..
首先,定义 foo 来采用 boost::function 类型
foo(boost::function
execFunction) { // 做某事 execFunction(args); // 做某事 }
然后,我根据我的需要给 foo() 不同的函数(作为参数)。
这行得通吗?或者你会建议反对吗?
欢迎任何评论。
编辑
次要相关问题是, 有时 execFunction 需要 1 个参数,有时需要 2 个参数。
我可以在这两种情况下使用 boost::function ,并且在不需要时忽略第二个参数。
有没有更干净的方法来做到这一点?
I want to call a function foo and let it behave differently.
(I'm adopting Strategy or Command pattern. They look similar to me.)
My overall plan is as follows..
First, define foo to take boost::function type
foo(boost::function<someType> execFunction) { // do something execFunction(args); // do something }
Then, I give foo() different functions(as argument) depending on what I want.
Would this work? or Would you advise against it?
Any comments are welcome.
edit
Minor related question is,
sometimes execFunction needs 1 argument, and other times it needs 2 arguments.
I could use boost::function for both cases and just ignore the 2nd argument when not needed.
Is there a cleaner way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这非常有效。但有时最好与
functor,以便您的调用者可以自由选择最适合他们的内容,并防止 boost::function 带来的小开销:
您还可以为特定的 boost:: 添加重载函数 通过引用获取对象。
并且可能也为了常量而重载。
对于接受不同数量的
boost::function
对象的情况,我会使用重载。如果您走这条路,请避免使用模板化版本,因为一旦您的用户尝试使用仿函数(而不是具有不同数量的boost::function
),您就会遇到麻烦。这个问题是可以解决的,但可能会变得混乱。您需要将调用分派给检测函子的数量并执行正确调用的东西。但正如您所说,模板是您的弱点,因此并不推荐这样做。This works very well. But sometimes it is preferable to work with a
functor so your callers are free to choose what is best for them and to prevent the smalll overhead that comes with a
boost::function
:You can also add an overload for the specific
boost::function
to take the object by reference.And possibly have that overloaded for constness as well.
For the case of accepting
boost::function
objects of different arity, I'd use overloading. If you go that route, avoid the templated version as you are going to run into trouble as soon as your users try to work with functors (as opposed toboost::function
with different arity). This trouble is resolvable, but can get messy. You would need to dispatch the call to something that detects the arity of the funtor and executes the proper call. But as you said that templates are your weak-point, this isn't really recommended.这是一种常见的模式,并且会起作用。我过去曾使用过它,并且很可能将来也会使用它。我尝试只在某些特定的用例中使用它,因为它大大减少了耦合,虽然这总体上是好的,但它使得遵循代码的控制流变得更加困难(即,如果你滥用它,那真的会很糟糕)除了在调试器中运行之外,很难找到程序的用途)。但对于特定的任务来说还是可以的。
如果你不需要存储
function<>
供以后使用,或者被调用方不需要任何运行时多态行为,你也可以考虑使用函数模板,这样调用者就不需要在执行函数之前转换为function<>
。That is a common pattern, and will work. I have used that in the past and will most probably use it in the future. I try to use it only in some specific use cases, as it reduces drastically coupling, and while that is good in general, it makes it harder to follow the flow of control from the code (i.e. if you abuse it, it will be really hard to find what your program is meant to do other than running it in the debugger). But for specific tasks it is fine.
If you do not need to store the
function<>
for later use, or any runtime polymorphic behavior on the callee side, you can also consider using a function template, so that callers don't need to convert tofunction<>
before executing your function.