c++ - boost signals2 - 通过插槽断开连接时出错

标签 c++ boost signals2

我想像 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;
}

...但调用断开连接时出现错误(请参阅 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')

我做错了什么?

最佳答案

问题是,std::function 没有 operator ==对于函数比较,仅针对函数和 nullptr,但它是必需的,因此您可以使用 boost::function,它具有比较运算符和 boost::bind

关于c++ - boost signals2 - 通过插槽断开连接时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34813260/

相关文章:

c++ - Boost 程序选项 - 从函数结果传递 arg 名称

c++ - 如何绑定(bind) SOCI 查询的输出?

c++ - Boost::signals2 传递无效数据

c++ - 如何在 Windows 中获取文件安全属性

c++ - 非源文件更改时触发构建

c++ - 使用 NDC 空间在近平面上投影点

c++ - CLion 无法识别 SDL2_image

c++ - 一般如何访问 Boost Graph 捆绑属性?