Example program that fails to compile (boost::bind and boost::function called for collection)

Below is a a test program with the purpose of calling member function for each element of the collection. Argument of this member function is a boost::function. I put compilation error after the source code. Any help will be greatly appreciated. #include <boost/bind.hpp> #include <boost/function.hpp> #include <boost/ptr_container/ptr_vector.hpp> typedef boost::function<void (int, int, const char*) > delegate; class A : public boost::noncopyable { public: commandhandler(delegate& f) {} }; template <typename C> class Acoll { typedef boost::ptr_vector<C> ptr_container; ptr_container m_data; public: typedef C CLIENT; ptr_container& operator() (){return m_data;} }; template <typename CLIENTS> class Test { CLIENTS& m_clients; public: delegate dosomething; Test(CLIENTS& val) : m_clients(val) {} void run() { std::for_each(m_clients().begin(), m_clients().end(), boost::bind(&CLIENTS::CLIENT::commandhandler, boost::ref(dosomething), _1 )); } }; int _tmain(int argc, _TCHAR* argv[]) { Acoll<A> coll; Test<Acoll<A> > test(coll); test.run(); return 0; } Error: 1>e:\libraries\boost\boost_1_37_0\boost\bind.hpp(292) : error C2664: 'R boost::_mfi::mf1<R,T,A1>::operator ()<boost::function<Signature>>(const U &,A1) const' : cannot convert parameter 2 from 'A' to 'boost::function<Signature> ' 1> with 1> [ 1> R=void, 1> T=A, 1> A1=delegate &, 1> Signature=void (int,int,const char *), 1> U=delegate 1> ] 1> and 1> [ 1> Signature=void (int,int,const char *) 1> ] 1> e:\libraries\boost\boost_1_37_0\boost\bind\bind_template.hpp(32) : see reference to function template instantiation 'void boost::_bi::list2<A1,A2>::operator ()<F,boost::_bi::list1<A &>>(boost::_bi::type<T>,F &,A &,int)' being compiled 1> with 1> [ 1> A1=boost::reference_wrapper<delegate>, 1> A2=boost::arg<1>, 1> F=boost::_mfi::mf1<void,A,delegate &>, 1> T=void, 1> A=boost::_bi::list1<A &> 1> ] 1> e:\program files\microsoft visual studio 9.0\vc\include\algorithm(29) : see reference to function template instantiation 'void boost::_bi::bind_t<R,F,L>::operator ()<T>(A1 &)' being compiled 1> with 1> [ 1> R=void, 1> F=boost::_mfi::mf1<void,A,delegate &>, 1> L=boost::_bi::list2<boost::reference_wrapper<delegate>,boost::arg<1>>, 1> T=A, 1> A1=A 1> ] 1> c:\test\t458\t458\t458.cpp(39) : see reference to function template instantiation '_Fn1 std::for_each<boost::void_ptr_iterator<VoidIter,T>,boost::_bi::bind_t<R,F,L>>(_ InIt,_InIt,_Fn1)' being compiled 1> with 1> [ 1> _Fn1=boost::_bi::bind_t<void,boost::_mfi::mf1<void,A,delegate &>,boost::_bi::list2<boost::reference_wrapper<delegate>,boost::arg<1>>>, 1> VoidIter=std::_Vector_iterator<void *,std::allocator<void *>>, 1> T=A, 1> R=void, 1> F=boost::_mfi::mf1<void,A,delegate &>, 1> L=boost::_bi::list2<boost::reference_wrapper<delegate>,boost::arg<1>>, 1> _InIt=boost::void_ptr_iterator<std::_Vector_iterator<void *,std::allocator<void *>>,A> 1> ] 1> c:\test\t458\t458\t458.cpp(37) : while compiling class template member function 'void Test<CLIENTS>::run(void)' 1> with 1> [ 1> CLIENTS=Acoll<A> 1> ] 1> c:\test\t458\t458\t458.cpp(47) : see reference to class template instantiation 'Test<CLIENTS>' being compiled 1> with 1> [ 1> CLIENTS=Acoll<A> 1> ]

AMDG Archie14 wrote:
delegate dosomething; Test(CLIENTS& val) : m_clients(val) {} void run() { std::for_each(m_clients().begin(), m_clients().end(), boost::bind(&CLIENTS::CLIENT::commandhandler, boost::ref(dosomething), _1 )); } };
The parameters to boost::bind are in the wrong order. Try: boost::bind(&CLIENTS::CLIENT::commandhandler, _1, boost::ref(dosomething))); In Christ, Steven Watanabe

Steven Watanabe <watanabesj <at> gmail.com> writes:
AMDG
Archie14 wrote:
delegate dosomething; Test(CLIENTS& val) : m_clients(val) {} void run() { std::for_each(m_clients().begin(), m_clients().end(), boost::bind(&CLIENTS::CLIENT::commandhandler, boost::ref(dosomething), _1 )); } };
The parameters to boost::bind are in the wrong order.
Try:
boost::bind(&CLIENTS::CLIENT::commandhandler, _1, boost::ref(dosomething)));
In Christ, Steven Watanabe
Steven, Thank you very much for the help. I have a follow-up question regarding boost::bind. Specifically - how can I bind one boost::function to another? Here is the short example that fails to compile: #include <boost/bind.hpp> #include <boost/function.hpp> #include <boost/ptr_container/ptr_vector.hpp> typedef boost::function<void (int, int, const char*) > delegateOne; typedef boost::function<void (int, const char*) > delegateTwo; class A : public boost::noncopyable { public: void commandhandler(delegateOne& f) { // I want to replace first argument in "delegateOne" with value 100. delegateTwo two = boost::bind(&f, 100, _2, _3); } }; int _tmain(int argc, _TCHAR* argv[]) { A a; delegateOne t; a.commandhandler(t); }

AMDG Archie14 wrote:
typedef boost::function<void (int, int, const char*) > delegateOne; typedef boost::function<void (int, const char*) > delegateTwo;
class A : public boost::noncopyable { public: void commandhandler(delegateOne& f) { // I want to replace first argument in "delegateOne" with value 100. delegateTwo two = boost::bind(&f, 100, _2, _3); } };
I assume that you want to store a reference to f in two? use boost::ref(f) instead of &f. In Christ, Steven Watanabe
participants (2)
-
Archie14
-
Steven Watanabe