question about using boost.function and boost.bind, V1.38
Can any one please explain to me why the former fails while the latter compilesThe definition: // usage in a ctor initialisation list ..., where m_button_monitor is of type ButtonMonitor, , m_button_monitor(boost::bind(boost::mem_fn(&CView::OnRun), this, ID_TOOLBUTTON_AUTO)) //definition 1 of ButtonMonitor that fails // the error message by MS VC9SP1: // error C2664: 'programmer_board::ButtonMonitor::ButtonMonitor(boost::function<Signatur e> &)' : //cannot convert parameter 1 from 'boost::_bi::bind_t<R,F,L>' to 'boost::function<Signature> &' // class ButtonMonitor { public: ButtonMonitor(boost::function<void()>& f) : m_f(f) { } void UpdateStatus(BUTTON_STATUS status) { m_f(); }; private: boost::function<void()>& m_f; }; //definition 2 of ButtonMonitor that works class ButtonMonitor { public: ButtonMonitor(const boost::function<void()>& f) : m_f(f) { } void UpdateStatus(BUTTON_STATUS status) { m_f(); }; private: const boost::function<void()>& m_f; }; Thanks,
// usage in a ctor initialisation list …, where m_button_monitor is of type ButtonMonitor,
, m_button_monitor(boost::bind(boost::mem_fn(&CView::OnRun), this, ID_TOOLBUTTON_AUTO))
//definition 1 of ButtonMonitor that fails
// the error message by MS VC9SP1:
// error C2664: 'programmer_board::ButtonMonitor::ButtonMonitor(boost::function<Signature> &)' :
//cannot convert parameter 1 from 'boost::_bi::bind_t<R,F,L>' to 'boost::function<Signature> &'
//
class ButtonMonitor
{
public:
ButtonMonitor(boost::function<void()>& f)
: m_f(f)
It seems that you try to initialize non-const reference with a temporary.
Igor R wrote:
// usage in a ctor initialisation list …, where m_button_monitor is of type ButtonMonitor,
, m_button_monitor(boost::bind(boost::mem_fn(&CView::OnRun), this,
In addition to Igor's response, the above can be simplified as: m_button_monitor(boost::bind(&CView::OnRun,this,ID_TOOLBUTTON_AUTO)) bind's first argument can directly be a member function address. Jeff
participants (3)
-
Igor R
-
Jeff Flinn
-
Tan, Tom (Shanghai)