2016-04-14 89 views
1

我想通過公共函數將回調函數連接到boost信號。我可以傳遞一個函數指針,但如果我嘗試使用std :: bind來傳遞成員函數,它將不會編譯。給我錯誤說沒有可行的轉換。我應該使用什麼類型的App :: SetCallback函數參數?將具有綁定成員函數的std :: bind對象傳遞給函數

#include <functional> 
#include <boost/signal.hpp> 
using namespace std::placeholders; // for _1, _2, _3... 

//plain simple call back function 
void SimpleCallback(int value) { 
//do nothing 
} 
//class contains a boost::signal, set callback through a public function 
class App { 
public: 
    App() : sig_() 
    {} 
    typedef boost::signal<void (int value)> SigType; 
    typedef std::function<void (int value)> CallbackFunType; 
    //connect signal to a callback function 
    void SetCallback(CallbackFunType callback) { 
     sig_.connect(callback); 
    } 
//private: //comment this out for testing purpose. 
    SigType sig_; //this is the boost::signal 
}; 

//class that has member callback function 
class MyCallback { 
public: 
    MyCallback(): 
    val(0), app() 
    {} 
    void MemberCb(int value){ 
     val = value; 
    } 
    void Connect() { 
     auto bind_fun = std::bind(&MyCallback::MemberCb, this, _1); 
     app.SetCallback(bind_fun); //this will not compile, no viable conversion 
     app.sig_.connect(bind_fun); //this is fine 
     app.SetCallback(SimpleCallback); //this is fine 
    } 
private: 
    int val; 
    App app; 
}; 

int main(int argc, char **argv) { 
    MyCallback my_cb; 
    my_cb.Connect(); 
    return 1; 
} 

---------------- UPDATE -----------------

閱讀增壓信號文檔更仔細地,我瞭解到我可以通過插槽類型。這解決了我的問題

#include <functional> 
#include <boost/signal.hpp> 
using namespace std::placeholders; // for _1, _2, _3... 

//plain simple call back function 
void SimpleCallback(int value) { 
//do nothing 
} 
//class contains a boost::signal, set callback through a public function 
class App { 
public: 
    App() : sig_() 
    {} 
    typedef boost::signal<void (int value)> SigType; 
    typedef SigType::slot_type CallbackFunType; 
    //typedef std::function<void (int value)> CallbackFunType; 
    //connect signal to a callback function 
    void SetCallback(CallbackFunType callback) { 
     sig_.connect(callback); 
    } 
//private: //comment this out for testing purpose. 
    SigType sig_; //this is the boost::signal 
}; 

//class that has member callback function 
class MyCallback { 
public: 
    MyCallback(): 
    val(0), app() 
    {} 
    void MemberCb(int value){ 
     val = value; 
    } 
    void Connect() { 
     auto bind_fun = std::bind(&MyCallback::MemberCb, this, _1); 
     app.SetCallback(bind_fun); //using SigType::slot_type 
     app.sig_.connect(bind_fun); 
     app.SetCallback(SimpleCallback); 
    } 
private: 
    int val; 
    App app; 
}; 

int main(int argc, char **argv) { 
    MyCallback my_cb; 
    my_cb.Connect(); 
    return 1; 
} 
+0

[編譯與升壓1.59和g ++ 5.3](http://coliru.stacked-crooked.com/a/38efbf5f17282ea8)。此外與蘋果LLVM 7.0.2對應的任何鏗鏘版本。你在建什麼? – rhashimoto

+0

@rhashimoto很高興知道它適用於您的平臺。我正在用Apple LLVM 7.1和Boost1.58構建。我會嘗試新版本的boost,看看它是否有效。 – DXM

+0

不完全是關鍵,但我可能會在這裏使用lambda而不是'std :: bind'。 – rhashimoto

回答

0

更仔細地閱讀升壓信號文檔,我瞭解到我可以通過插槽類型。這解決了我的問題

#include <functional> 
#include <boost/signal.hpp> 
using namespace std::placeholders; // for _1, _2, _3... 

//plain simple call back function 
void SimpleCallback(int value) { 
//do nothing 
} 
//class contains a boost::signal, set callback through a public function 
class App { 
public: 
    App() : sig_() 
    {} 
    typedef boost::signal<void (int value)> SigType; 
    typedef SigType::slot_type CallbackFunType; 
    //typedef std::function<void (int value)> CallbackFunType; 
    //connect signal to a callback function 
    void SetCallback(CallbackFunType callback) { 
     sig_.connect(callback); 
    } 
//private: //comment this out for testing purpose. 
    SigType sig_; //this is the boost::signal 
}; 

//class that has member callback function 
class MyCallback { 
public: 
    MyCallback(): 
    val(0), app() 
    {} 
    void MemberCb(int value){ 
     val = value; 
    } 
    void Connect() { 
     auto bind_fun = std::bind(&MyCallback::MemberCb, this, _1); 
     app.SetCallback(bind_fun); //using SigType::slot_type 
     app.sig_.connect(bind_fun); 
     app.SetCallback(SimpleCallback); 
    } 
private: 
    int val; 
    App app; 
}; 

int main(int argc, char **argv) { 
    MyCallback my_cb; 
    my_cb.Connect(); 
    return 1; 
} 
相關問題