2017-08-07 12 views
5

के बाद से मैं a project कि नए बूस्ट 1.63 के खिलाफ बूस्ट 1.55 के खिलाफ लिखा गया था संकलित करने के लिए कोशिश कर रहा था, और मैं वास्तव में एक अजीब bind/function से संबंधित त्रुटि में चल रहा हूँ। यहाँ पूरा, सरलीकृत परीक्षण का मामला है:बढ़ावा :: बाँध/बढ़ावा में अजीब व्यवहार परिवर्तन :: समारोह 1.55

#include <boost/bind.hpp> 
#include <boost/function.hpp> 

template < typename Arg1 = int, typename Arg2 = int, typename Arg3 = int > 
class foo 
{ 
public: 
    using function_t = boost::function3< void, Arg1, Arg2, Arg3 >; 

    void set_function(function_t f) 
    { 
    func_ = f; 
    } 

private: 
    function_t func_; 
}; 

class bar 
{ 
public: 
    bar() 
    { 
    foo_.set_function(boost::bind(&bar::func, this, _1, _2)); 
    } 

private: 
    void func(int const&, int&) {} 

    foo< int, int > foo_; 
}; 

int main() 
{ 
    bar x; 
    return 0; 
} 

... और त्रुटियों में से कुछ का चयन के टुकड़े मैं हो रही है:

/usr/include/boost/bind/bind.hpp:398:35: error: no match for call to ‘(boost::_mfi::mf2<void, bar, const int&, int&>) (bar*&, int, int)’ 
/usr/include/boost/bind/mem_fn_template.hpp:278:7: note: candidate: R boost::_mfi::mf2<R, T, A1, A2>::operator()(T*, A1, A2) const [with R = void; T = bar; A1 = const int&; A2 = int&] <near match> 
/usr/include/boost/bind/mem_fn_template.hpp:278:7: note: conversion of argument 3 would be ill-formed: 
/usr/include/boost/bind/bind.hpp:398:35: error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’ 
/usr/include/boost/bind/mem_fn_template.hpp:283:25: note: candidate: template<class U> R boost::_mfi::mf2<R, T, A1, A2>::operator()(U&, A1, A2) const [with U = U; R = void; T = bar; A1 = const int&; A2 = int&] 
/usr/include/boost/bind/mem_fn_template.hpp:283:25: note: template argument deduction/substitution failed: 
/usr/include/boost/bind/bind.hpp:398:35: note: cannot convert ‘(& a)->boost::_bi::rrlist3<int, int, int>::operator[](boost::_bi::storage3<boost::_bi::value<bar*>, boost::arg<1>, boost::arg<2> >::a3_)’ (type ‘int’) to type ‘int&’ 
/usr/include/boost/bind/mem_fn_template.hpp:291:25: note: candidate: template<class U> R boost::_mfi::mf2<R, T, A1, A2>::operator()(const U&, A1, A2) const [with U = U; R = void; T = bar; A1 = const int&; A2 = int&] 
/usr/include/boost/bind/mem_fn_template.hpp:291:25: note: template argument deduction/substitution failed: 
/usr/include/boost/bind/bind.hpp:398:35: note: cannot convert ‘(& a)->boost::_bi::rrlist3<int, int, int>::operator[](boost::_bi::storage3<boost::_bi::value<bar*>, boost::arg<1>, boost::arg<2> >::a3_)’ (type ‘int’) to type ‘int&’ 
/usr/include/boost/bind/mem_fn_template.hpp:299:7: note: candidate: R boost::_mfi::mf2<R, T, A1, A2>::operator()(T&, A1, A2) const [with R = void; T = bar; A1 = const int&; A2 = int&] 
/usr/include/boost/bind/mem_fn_template.hpp:299:7: note: no known conversion for argument 1 from ‘bar*’ to ‘bar&’ 

अगर मैं एक और मशीन को बढ़ावा 1.55 है पर कोशिश करते हैं, यह संकलित बस ठीक। (बूस्ट 1.55 के निर्माण की ओर इशारा करते समय परियोजना उसी मशीन पर भी ठीक हो गई है, इसलिए समस्या संकलक प्रतीत नहीं होती है।) तो जाहिर है, बूस्ट में कुछ बदल गया है जो इसे विफल होने के कारण नया है।

मैंने यह कोड नहीं लिखा था, न कि मैं boost::function और boost::bind की गड़बड़ी से परिचित हूं। अगर कोई मुझे समझा सकता है कि यह नए बूस्ट के साथ क्यों टूट रहा है, और या तो ए) इसे कैसे ठीक किया जाए, या बी) उपर्युक्त कोड क्यों टूटा हुआ है, तो इसकी सराहना की जाएगी!

उत्तर

6

समस्या यह है कि boost::function फ़ंक्शन हस्ताक्षर से मेल खाने में कठोर हो गया है (मुझे लगता है कि std::function के व्यवहार से अधिक बारीकी से मिलान करना है)। आपका फ़ंक्शन boost::function3< void, Arg1, Arg2, Arg3 > घोषित किया गया है, हालांकि, जिस फ़ंक्शन को आप बाध्यकारी कर रहे हैं वह int const& और int& लेता है।

तरीके इसे ठीक करने के एक जोड़े हैं। आप:

  1. बदलें bar में foo<int, int> की instantion foo<int const&, int&> किया जाना है।
  2. function_t की घोषणा बदलें boost::function3<void, Arg1, Arg2 const&, Arg3&> किया जाना है।
  3. मूल्य द्वारा अपने तर्कों को स्वीकार करने के bar::func के हस्ताक्षर बदलें।
+0

धन्यवाद। मैं कहूंगा कि (3) असंभव है क्योंकि परेशानी पैरामीटर आउटपुट पैरामीटर हैं, लेकिन गहरी खुदाई करते हैं, AFAICT ए) इस पैटर्न का उपयोग करने वाला वास्तविक कोड संभावित रूप से एक वैध संदर्भ पैरामीटर का उत्पादन नहीं कर सकता है जिसके साथ अंततः संग्रहित फ़ंक्शन का आह्वान किया जा सकता है, और बी) वास्तविक प्रकार वैसे भी एक संदर्भ कंटेनर है, और इसलिए डीटीआरटी भी मूल्य/प्रति द्वारा पारित किया जाएगा। तो AFAICT, (3) वास्तव में असली कोड के लिए सही तय है। (रिकॉर्ड के लिए नोट: 'const &' तर्क अभी भी अनुकूलित किए जा सकते हैं; केवल संदर्भ पैरामीटर को बदलने की आवश्यकता है।) – Matthew

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