我想像 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/