में भी किया जाता रहा Boost.Asio दस्तावेज में विभिन्न ट्यूटोरियल कोशिश कर रहा था और C++ 11 लोगों के साथ बढ़ावा घटकों को बदलने के लिए कोशिश की जा। हालांकि, मुझे Timer.5 - Synchronising handlers in multithreaded programs में std :: bind का उपयोग करके त्रुटि मिली।क्यों std नहीं कर सकते :: बाँध और बढ़ावा देने :: बाँध इस Boost.Asio ट्यूटोरियल
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer { /* Not relevent here */ };
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;
}
मैं std::bind
द्वारा std::thread
और boost::bind
द्वारा boost::thread
को बदलने के लिए करने की कोशिश की: यहाँ कोड प्रस्तावित है।
#include <functional>
#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer { /* Not relevent here */ };
int main() {
boost::asio::io_service io;
printer p(io);
std::thread t(std::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
}
जब जीसीसी 4.7 के साथ संकलन, मैं इस संकलन समय त्रुटि मिली:
g++ -std=c++0x main.cpp -lboost_system -lboost_date_time -lpthread
main.cpp: In function ‘int main()’:
main.cpp:52:60: erreur: no matching function for call to ‘bind(<unresolved overloaded function type>, boost::asio::io_service*)’
main.cpp:52:60: note: candidates are:
/usr/include/c++/4.6/functional:1444:5: note: template<class _Functor, class ... _ArgTypes> typename std::_Bind_helper::type std::bind(_Functor&&, _ArgTypes&& ...)
/usr/include/c++/4.6/functional:1471:5: note: template<class _Result, class _Functor, class ... _ArgTypes> typename std::_Bindres_helper::type std::bind(_Functor&&, _ArgTypes&& ...)
कहाँ इस त्रुटि से आ रही है, तो खाता है कि मैं किसी भी boost::asio::placeholders
का उपयोग नहीं किया को ध्यान में रखकर यहाँ मेरी कोड है (जैसा कि इस stackoverflow प्रश्न Should std::bind be compatible with boost::asio? में समझाया गया है)?
संभावित डुप्लिकेट: http://stackoverflow.com/questions/8924149/should-stdbind-be-compatible-with-boostasio देखें) – mark
चूंकि आप पहले से ही C++ 11 का उपयोग कर रहे हैं: lambdas एक विकल्प हो सकता है आपके लिए 'std :: bind'', उदाहरण के लिए, 'std :: thread t ([& io]() {io.run();});' यह अधिभार संकल्प से पूरी तरह से बचें। – mavam