2016-01-15 43 views
1

我想通過一個插槽斷開像boost signals2 documentation的:升壓signals2 - 錯誤時傳遞插槽斷開

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')

我錯了什麼?

回答

1

問題是,std::function沒有operator ==的功能比較,只對功能和nullptr,但它是必需的,所以你纔可以使用boost::function,它具有比較運營商與boost::bind

+0

使用boost :: bind而不是std :: bind。 –

+0

我改變了使用'boost :: function'和'boost :: bind'的代碼但是現在我得到了另一個錯誤(請參閱更新錯誤信息) –

+0

@ThomasFischer您真的需要使用'sig.disconnect(slot) ? [這裏]描述的方法(http://www.boost.org/doc/libs/1_60_0/doc/html/signals2/tutorial.html#idm45555132618416)似乎更可靠(http://coliru.stacked-crooked的.com /一個/ be7fb8d180a8dd6a)。 – llonesmiz