2012-07-23 20 views
5

के साथ बूस्ट :: थ्रेड बनाना मेरे पास निम्नलिखित दो कोड सेगमेंट हैं। पहला ब्लॉक संकलित करता है और अपेक्षित काम करता है। हालांकि दूसरा ब्लॉक संकलित नहीं करता है।एक std :: shared_ptr ऑब्जेक्ट उदाहरण

मेरा प्रश्न यह है कि नीचे दिए गए कोड को दिए गए कोड को दिए गए किसी ऑब्जेक्ट के उदाहरण के आधार पर थ्रेड बनाने का प्रयास करते समय क्या किया गया है, जिसे shared_ptr द्वारा प्रॉक्सी किया जा रहा है?

#include <iostream> 
#include <new> 
#include <memory> 

#include <boost/thread.hpp> 

struct foo 
{ 
    void boo() {} 
}; 

int main() 
{ 
    //This works 
    { 
     foo* fptr = new foo; 
     boost::thread t(&foo::boo,fptr); 
     t.join(); 
     delete fptr; 
    } 

    //This doesn't work 
    { 
     std::shared_ptr<foo> fptr(new foo); 
     boost::thread t(&foo::boo,fptr); 
     t.join(); 
    } 

    return 0; 
} 

संकलक त्रुटि:

Error 5 error C2784: 'T *boost::get_pointer(T *)' : could not deduce template argument for 'T *' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 3 error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 4 error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 8 error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 9 error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 1 error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 2 error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 6 error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 7 error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 10 error C2784: 'T *boost::get_pointer(const boost::intrusive_ptr<T> &)' : could not deduce template argument for 'const boost::intrusive_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 
Error 11 error C2784: 'T *boost::get_pointer(const boost::intrusive_ptr<T> &)' : could not deduce template argument for 'const boost::intrusive_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp 40 1 htest 

उत्तर

9

समस्या यह है कि boost::threadboost::mem_fn पर निर्भर करता है सदस्य कार्यों को संभालने के लिए, और boost::mem_fn (या कम से कम संस्करण का उपयोग कर रहे हैं) कैसे उपयोग करने के लिए पता नहीं है एक सदस्य फ़ंक्शन को आमंत्रित करने के लिए std::shared_ptr क्योंकि यह आपको boost::shared_ptr या आपकी त्रुटि सूची में अन्य स्मार्ट पॉइंटर प्रकारों के असंख्य में से एक का उपयोग करने की अपेक्षा करता है।

कच्चा सूचक काम करता है क्योंकि boost::mem_fn में पहले से ही अधिभार है। समाधान boost::shared_ptr या std::mem_fn का उपयोग करना है। बाद के काम करता है क्योंकि std::mem_fn जानता है (<boost/mem_fn.hpp> से पहले शामिल हो जाता है) std::shared_ptr

boost::thread t(std::mem_fn(&foo::boo), fptr);

+0

उत्कृष्ट जो ठीक काम करता है! धन्यवाद। –

3

डेव एस के जवाब के लिए एक वैकल्पिक इस परिभाषित करने के लिए है के साथ बातचीत करने के लिए कैसे:

namespace boost 
{ 
    template<typename T> 
    inline T* 
    get_pointer(const std::shared_ptr<T>& p) 
    { return p.get(); } 
} 

कि "सिखाता है" boost::mem_fnstd::shared_ptr से कच्चे सूचक प्राप्त करने के लिए।

C++ में 11 std::mem_fn, किसी भी सूचक की तरह प्रकार के साथ काम करने के लिए बस इसे अर्थात *fptr अपसंदर्भन के लिए आवश्यक है, लेकिन boost::mem_fn बजाय *boost::get_pointer(fptr) उपयोग करता है। मुझे नहीं पता कि यह बूस्ट के नवीनतम संस्करण में तय है या नहीं, लेकिन मैं तर्क दूंगा कि यह पता लगाने के लिए कि get_pointer काम करेगा या नहीं, इसे एसएफआईएनएई का उपयोग करना चाहिए, और इसे अन्यथा अस्वीकार करना चाहिए।

संबंधित मुद्दे