2013-11-23 53 views
1

在這部分代碼從example爲什麼'boost :: bind()`不能用`std :: bind()`替換?

int main() 
{ 
    boost::asio::io_service io; 
    printer p(io); 
    boost::thread t(boost::bind(&boost::asio::io_service::run, &io)); 
    io.run(); 
    t.join(); 

    return 0; 
} 

如果我更換boost::bind(&boost::asio::io_service::run, &io)std::bind(&boost::asio::io_service::run, &io)我得到的編譯錯誤:

.../usr/lib/c++/v1/functional:1843:1: note: candidate template 
    ignored: couldn't infer template argument '_Fp' 
bind(_Fp&& __f, _BoundArgs&&... __bound_args) 
^ 
/usr/lib/c++/v1/functional:1852:1: note: candidate template 
    ignored: couldn't infer template argument '_Rp' 
bind(_Fp&& __f, _BoundArgs&&... __bound_args) 
^ 
1 error generated. 

爲什麼這個錯誤發生?

爲什麼std::bind(&printer::print1, &p)有效,但std::bind(&boost::asio::io_service::run, &io)不起作用?

+0

添加錯誤信息的所有行。 –

+0

'boost :: thread'有一個轉發構造函數,所以你根本不需要綁定:'boost :: thread t(&boost :: asio :: io_service :: run,&io);' –

回答

3

編譯器告訴你它找不到第一個參數的類型爲std::bind

如果你看看io_service::run,你會發現它已經過載了。編譯器有一個選擇,這是編譯器不能確定類型的一個可能的原因。要測試此操作,您可以使用演員表:

std::bind(static_cast<size_t (boost::asio::io_service::*)()>(&boost::asio::io_service::run), &io) 

這種方式使得選擇在代碼中顯式化。

使用現代編譯器,您根本不需要使用bind。你可以做這樣的事情:

[&]() { io.run(); } 

這避免了所有與std::bind模板的問題。您需要考慮變量生存期和副本與參考。

+0

'謝謝,但它也可以不是強制轉換成'std :: function ',但它可以工作,當我的情況下'std :: size_t(boost :: asio :: io_service :: *)()',boost :: bind可以處理過載函數,也許lambda函數更有幫助! – user2763477

+0

'std :: function '沒有構造函數接受指向成員函數的指針(更不用說在這裏創建'std :: function'是絕對不必要的開銷) –

+0

@ IgorR:是的,你是對的,我在文本中寫了「cast」,但是後來完全輸入了其他內容!將修復代碼 – janm