Reputation: 6503
I have an instance of a noncopyable object, that I want to use with a boost signal2. The connect method expects my functor to be copyable. Is there a way to work this around? How do I remove the comment in the code below?
#include <iostream>
#include <boost/signals2.hpp>
struct Noncopyable
{
Noncopyable() {};
void operator()() { std::cerr << "hi Noncopyable" << std::endl; }
private:
Noncopyable(Noncopyable const&);
};
int main(void)
{
Noncopyable no_copy;
boost::signals2::signal<void ()> sig;
//sig.connect(no_copy);
sig();
}
Is there a way to pass a reference to no_copy
object into connect
method?
Upvotes: 0
Views: 370
Reputation: 15075
Use boost::ref
(or std::ref
) function to pass such an object by reference:
#include <iostream>
#include <boost/signals2.hpp>
#include <boost/ref.hpp>
struct Noncopyable
{
Noncopyable() {};
void operator()() { std::cerr << "hi Noncopyable" << std::endl; }
private:
Noncopyable(Noncopyable const&);
};
int main(void)
{
Noncopyable no_copy;
boost::signals2::signal<void ()> sig;
sig.connect(boost::ref(no_copy));
sig();
}
Of course, ensure that no_copy
object lives at least as long as it's connected to sig
.
Upvotes: 1