1

I want to pass a slot to disconnect like in the boost signals2 documentation:

Pass slot to disconnect: in this interface model, the disconnection of a slot connected with sig.connect(typeof(sig)::slot_type(slot_func)) is performed via sig.disconnect(slot_func).

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

class Test {
public:
    Test();
    using Signal = boost::signals2::signal<void ()>;
    void slot();
    void test();
};

Test::Test() {
    boost::function<void ()> boundSlot = boost::bind(&Test::slot, this);
    Signal sig;
    sig.connect(Signal::slot_type(boundSlot));
    sig();
    sig.disconnect(boundSlot);
};

void Test::slot() {
    std::cout << "slot()" << std::endl; 
};

int main()
{
   Test aTest;
   return 0;
}

... but I got an error when calling disconnect (see http://cpp.sh/45q6):

error: ambiguous overload for 'operator=='
(operand types are 'boost::signals2::slot >::slot_function_type {aka boost::function}'
and 'const boost::function')

What do I wrong?

Thomas Fischer
  • 540
  • 4
  • 16

1 Answers1

1

Problem is, that std::function has no operator == for function compare, only for function and nullptr, but it's required, so you just can use boost::function, which has compare operator and boost::bind.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • Use boost::bind instead of std::bind. –  Jan 15 '16 at 14:48
  • I changed the code to use `boost::function` and `boost::bind` But I get now an other error (see updated error message) – Thomas Fischer Jan 15 '16 at 15:23
  • @ThomasFischer Do you really need to use `sig.disconnect(slot)`? The approach described [here](http://www.boost.org/doc/libs/1_60_0/doc/html/signals2/tutorial.html#idm45555132618416) seems more [reliable](http://coliru.stacked-crooked.com/a/be7fb8d180a8dd6a). – llonesmiz Jan 15 '16 at 15:45
  • @cv_and_he Thats what I did before (and it works) – I was just curious to try out the documented "Pass slot to disconnect" approach :-/ – Thomas Fischer Jan 15 '16 at 15:58