2013-07-01 40 views
6

我正在探索g ++-4.7(Ubuntu/Linaro 4.7.3-2ubuntu〜12.04,具體)對C++ 11的支持,我似乎在尋找差異。我應該看到std :: bind和boost :: bind之間的顯着差異嗎?

特別是,如果我註釋掉#include <boost/bind.hpp>和系統地在升壓ASIO異步客戶端的例子(從http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client/async_client.cpp截取)std::bind取代的boost::bind出現,程序不再編譯。

對此有何解釋?

+2

你能請一個失敗的更換(最好是單一的替換),並與編譯器錯誤沿着發佈修改後的代碼? – Praetorian

+0

我記得看到了嵌套綁定的不同行爲。現在不記得具體細節。然而,這並不是你在這裏遇到的。 – sehe

回答

7
#include <functional> 
namespace boost { 
    namespace asio { 
     namespace stdplaceholders { 
      static decltype (:: std :: placeholders :: _1) & error = :: std :: placeholders :: _1; 
      static decltype (:: std :: placeholders :: _2) & bytes_transferred = :: std :: placeholders :: _2; 
      static decltype (:: std :: placeholders :: _2) & iterator = :: std :: placeholders :: _2; 
      static decltype (:: std :: placeholders :: _2) & signal_number = :: std :: placeholders :: _2; 
     } 
    } 
} 

,並使用boost::asio::stdplaceholders::*代替boost::asio::placeholders::*

+0

工作就像一個魅力,非常感謝! –

4

看起來boost::asio::placeholders不能與std::bind結合使用。在你掛例如,boost::bind第一次調用發生在下面的代碼:

resolver_.async_resolve(query, 
    boost::bind(&client::handle_resolve, this, 
     boost::asio::placeholders::error, 
     boost::asio::placeholders::iterator)); 

只需更換boost::bindstd::bind導致一堆錯誤。爲了使它編譯,你需要用std::placeholders替換boost::asio::placeholders

resolver_.async_resolve(query, 
    std::bind(&client::handle_resolve, this, 
     std::placeholders::_1, 
     std::placeholders::_2)); 

請注意,我沒有驗證代碼在進行這些更改後在功能上是相同的,只是它編譯了。