Using a boost signal within boost::bind
Asked Answered
R

1

5

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?

Readymix answered 21/9, 2010 at 22:17 Comment(0)
C
13

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);
}
Chiropractic answered 22/9, 2010 at 1:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.