C++ 如何使用函子将boost信号2与类的方法断开?

C++ 如何使用函子将boost信号2与类的方法断开?,c++,boost,publish-subscribe,C++,Boost,Publish Subscribe,当从boost::signals2断开插槽(即类方法)时,我没有得到预期的行为。我的术语可能不适用,因此我将在下面提供一个最小工作示例(MWE),演示我所看到的和我所期望的。简短的版本是,我正在断开与信号的连接,但它仍在那里。如果我用一个独立的函数来实现这一点,那么一切都会很好,当我使用一个类方法时,我会遇到这种行为 任何帮助都将不胜感激 >树 >cat SCO结构 >cat main.cpp >。/main 我确实使用connect方法返回的boost::signals2::connect

当从boost::signals2断开插槽(即类方法)时,我没有得到预期的行为。我的术语可能不适用,因此我将在下面提供一个最小工作示例(MWE),演示我所看到的和我所期望的。简短的版本是,我正在断开与信号的连接,但它仍在那里。如果我用一个独立的函数来实现这一点,那么一切都会很好,当我使用一个类方法时,我会遇到这种行为

任何帮助都将不胜感激

>树

>cat SCO结构

>cat main.cpp

>。/main


我确实使用connect方法返回的boost::signals2::connection对象断开了与boost::signals2::signal的连接。

使用
作用域连接重新实现:

#include <boost/signals2.hpp>
#include <iostream>
struct foo {
    void bar(int n) {
        std::cout << "Called foo::bar with " << n << std::endl;
    }
};



typedef boost::function<void(int)> Signal_f;
int main() {

    using boost::signals2::scoped_connection;

    foo f;
    boost::signals2::signal< void(int) > my_signal;
    Signal_f functor = boost::bind(&foo::bar, f, _1);
    std::cout << "Created signal, and it has "
              << my_signal.num_slots() << " subscribers." << std::endl;

    // the scoped_connection object is RAII
    auto con = scoped_connection(my_signal.connect(functor));

    std::cout << "Subscribed to signal, and it has "
              << my_signal.num_slots() << " subsciber." << std::endl;
    my_signal(1);

    // disconnect the connection object, not the signal
    con.disconnect();
    std::cout << "Un-Subscribed to signal, and it now has "
              << my_signal.num_slots()
              << " subscibers." << std::endl;
    my_signal(2);
    return 0;
}

我更喜欢自动处理东西的作用域连接我也喜欢作用域连接,但我希望在我的完整实现中不必管理另一个对象。必须创建函子对象,最好使用它来断开连接。如果没有其他人可以提供如何使用functor断开连接的指导,我会接受这个答案,因为“你不能那样做,改为这样做。”@KennethE.Bellock还有另一个选项-signals2让你有机会使用
connect\u extended
,这会将连接的引用传递给插槽处理程序。
Program('main.cpp')
#include <boost/signals2.hpp>
#include <iostream>
struct foo {
    void bar(int n) {
        std::cout << "Called foo::bar with " << n << std::endl;
    }
};
typedef boost::function<void(int)> Signal_f;
int main() {

    foo f;
    boost::signals2::signal< void(int) > my_signal;
    Signal_f functor = boost::bind(&foo::bar, f, _1);
    std::cout << "Created signal, and it has "
              << my_signal.num_slots() << " subscribers." << std::endl;
    my_signal.connect(functor);
    std::cout << "Subscribed to signal, and it has "
              << my_signal.num_slots() << " subsciber." << std::endl;
    my_signal(1);
    my_signal.disconnect(&functor);
    std::cout << "Un-Subscribed to signal, but it still has "
              << my_signal.num_slots()
              << " subsciber, and it should not have any now." << std::endl;
    my_signal(2);
    return 0;
}
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o main.o -c main.cpp
g++ -o main main.o
scons: done building targets.
Created signal, and it has 0 subscribers.
Subscribed to signal, and it has 1 subsciber.
Called foo::bar with 1
Un-Subscribed to signal, but it still has 1 subsciber, and it should not have any now.
Called foo::bar with 2
#include <boost/signals2.hpp>
#include <iostream>
struct foo {
    void bar(int n) {
        std::cout << "Called foo::bar with " << n << std::endl;
    }
};



typedef boost::function<void(int)> Signal_f;
int main() {

    using boost::signals2::scoped_connection;

    foo f;
    boost::signals2::signal< void(int) > my_signal;
    Signal_f functor = boost::bind(&foo::bar, f, _1);
    std::cout << "Created signal, and it has "
              << my_signal.num_slots() << " subscribers." << std::endl;

    // the scoped_connection object is RAII
    auto con = scoped_connection(my_signal.connect(functor));

    std::cout << "Subscribed to signal, and it has "
              << my_signal.num_slots() << " subsciber." << std::endl;
    my_signal(1);

    // disconnect the connection object, not the signal
    con.disconnect();
    std::cout << "Un-Subscribed to signal, and it now has "
              << my_signal.num_slots()
              << " subscibers." << std::endl;
    my_signal(2);
    return 0;
}
Created signal, and it has 0 subscribers.
Subscribed to signal, and it has 1 subsciber.
Called foo::bar with 1
Un-Subscribed to signal, and it still has 0 subscibers.