Using a boost signal within boost::bind
I'm trying to wrap triggering for a boost::signal into a boost::bind object. So what I want is to invoke the signal with some pre-packaged arguments when the boost::function is called.
What I have is this:
boost::signals2::signal<void(int)> sig;
boost::function<void()> f = boost::bind(
&(sig.operator()), &sig, 10);
But this doesn't work. I get the following error: error: no matching function for call to bind(, ...
I also tried this:
boost::function<void()> f = boost::bind(
(void(boost::signals2::signal<void(int)>::*)(int))
&(sig.operator()), &sig, 10);
But then I get "address of overloaded function with no contextual type information."
So what's the right syntax for this?开发者_如何转开发
An instance of boost::signals2::signal is a function object (a.k.a. functor), and can be bound directly, as described here. The only problem in this case is that a signal is noncopyable, and so it cannot be copied into the object returned by bind. So you first have to wrap it with a boost::ref. Here's an example:
#include <boost/signals2.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
int main(void)
{
boost::signals2::signal<void(int)> sig;
boost::function<void()> f = boost::bind(boost::ref(sig), 10);
}
精彩评论