कुछ सी ++ ऑब्जेक्ट्स में कोई कॉपी कन्स्ट्रक्टर नहीं है, लेकिन कन्स्ट्रक्टर को ले जाया गया है। उदाहरण के लिए, boost :: वादा। मैं अपने ऑब्जेक्ट कन्स्ट्रक्टर का उपयोग करके उन वस्तुओं को कैसे बांध सकता हूं?बूस्ट :: गैर-प्रतिलिपि वाले पैरा के साथ बाध्य कैसे करें, उदाहरण के लिए boost :: वादा?
#include <boost/thread.hpp>
void fullfil_1(boost::promise<int>& prom, int x)
{
prom.set_value(x);
}
boost::function<void()> get_functor()
{
// boost::promise is not copyable, but movable
boost::promise<int> pi;
// compilation error
boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1);
// compilation error as well
boost::function<void()> f_set_one = boost::bind(&fullfil_1, std::move(pi), 1);
// PS. I know, it is possible to bind a pointer to the object instead of
// the object itself. But it is weird solution, in this case I will have
// to take cake about lifetime of the object instead of delegating that to
// boost::bind (by moving object into boost::function object)
//
// weird: pi will be destroyed on leaving the scope
boost::function<void()> f_set_one = boost::bind(&fullfil_1, boost::ref(pi), 1);
return f_set_one;
}
इस प्रकार स्थानांतरित कर सकते हैं, यह क्या आप बाँध वस्तुओं के लिए दे रहे हैं पर निर्भर करता है। उदाहरण के लिए, यदि आप संकेतों का उपयोग कर रहे हैं, तो आप कनेक्शन ऑब्जेक्ट को सहेज सकते हैं, और ऑब्जेक्ट के डॉटोर में डिस्कनेक्ट कर सकते हैं। यदि आप सिग्नल का उपयोग नहीं कर रहे हैं, तो आप कुछ समान विकसित कर सकते हैं, या पॉइंटर को shared_ptr में लपेट सकते हैं। – Kyle
बढ़ावा :: वादा, वास्तव में, एक साझा_प्टर और एक बूल है। यह अजीब लगता है कि इसे ढेर पर आवंटित किया जाना चाहिए और एक और shared_ptr के साथ ट्रैक किया जाना चाहिए। – user222202