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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With