Search code examples
c++boostboost-thread

Boost signals2 connect() call fails to compile if slot object contains mutex & condition variable


Compiler: MSVS 2008

Boost: 1.49

Code:

#include <boost/signals2.hpp>
#include <boost/thread.hpp>

class SigOwner
{
public:
    typedef boost::signals2::signal<void (int)> OSig;
    OSig _signal;

    void doConnect(OSig::slot_type slot) { _signal.connect(slot); }
};

class SigUser
{
public:
#if defined(FAIL2)
    boost::mutex sync;
#endif
#if defined(FAIL1)
    boost::condition_variable evSig;
#endif

    void setup(SigOwner &so)
    {
        so.doConnect(*this);   // failure 1 traces back to this line
    }

    void operator()(int value) // signature to make SigUser a slot
    {
    }
};                             // failure 2 flags on this line

As presented, this compiles OK.

If I define FAIL1 (with or without FAIL2), a compiler error occurs within signals2/slot_template.hpp: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const SigUser' (or there is no acceptable conversion)

I don't know why the *this is being considered const.

If I define FAIL2 (without defining FAIL1) a compiler error occurs at the indicated line: error C2248: 'boost::mutex::mutex' : cannot access private member declared in class 'boost::mutex'

I don't know what private member is trying to be accessed.

Can anyone provide me a clue? Preferably a clue that allows me to define both FAIL1 and FAIL2 and get a successful compilation.


Solution

  • Neither mutex nor condition_variable are copiable, so your SigUser is not copiable, so you cannot pass it to doConnect this way. One way to work-around this is to define sync and evSig as (smart)pointers.