
Hi, I'd like to be able to bind an object to to a member function argument, like the example below. NB: if I make not_ok()'s first argument an int and pass an int variable to bind, gcc will buy it. I've tried this every way that makes any sense to me and I have not been able to get it to work. I'm using gcc 2.95.3. Thanks, Bill #include <boost/bind.hpp> #include <boost/function.hpp> class D { private: int _d; public: D() :_d(0) {} int d() const { return _d; } void d(int i) { _d = i; } }; typedef boost::function<int, int> Foo; class A { private: Foo _f; public: A(Foo f) :_f(f) {} int doit(int i) { return _f(i); } }; class B { public: int not_ok(const D *d, int j) { return d->d() + j; } }; class C { public: int ok(int i) { return i * i; } }; int fooBar(int j); static Foo g = fooBar; int fooBar(int j) { B *b = new B; C *c = new C; Foo f1 = boost::bind(&C::ok, c, _1); A a1(f1); int i = 5; D *d = new D; d->d(3); Foo f2 = boost::bind(&B::not_ok, b, _1, d); //<----- A a2(f2); return a1.doit(i) + a2.doit(2); } /usr/local/devl/src/incl/boost/bind.hpp: In method `int boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> >::operator ()<int, boost::_mfi::mf2<int,B,const D *,int>, boost::_bi::list1<int &>
(boost::_bi::type<int>, boost::_mfi::mf2<int,B,const D *,int>, boost::_bi::list1<int &> &) const': /usr/local/devl/src/incl/boost/bind/bind_template.hpp:33: instantiated from `boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > >::operator ()<int>(int &)' /usr/local/devl/src/incl/boost/function/function_template.hpp:110: instantiated from `boost::detail::function::function_obj_invoker1<boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > ,int,int>::invoke(boost::detail::function::any_pointer, int)' /usr/local/devl/src/incl/boost/function/function_template.hpp:437: instantiated from `boost::function1<int,int,boost::empty_function_policy,boost::empty_function_mixin,allocator<boost::function_base> ::assign_to<boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > > (boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > >, boost::detail::function::function_obj_tag)' /usr/local/devl/src/incl/boost/function/function_template.hpp:388: instantiated from `boost::function1<int,int,boost::empty_function_policy,boost::empty_function_mixin,allocator<boost::function_base> ::assign_to<boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > > (boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > >)' /usr/local/devl/src/incl/boost/function/function_template.hpp:274: instantiated from `boost::function1<int,int,boost::empty_function_policy,boost::empty_function_mixin,allocator<boost::function_base> ::function1<boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > > (boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > >)' /usr/local/devl/src/incl/boost/function.hpp:459: instantiated from `boost::function<int,int,boost::detail::function::unusable,boost::detail::function::unusable,boost::detail::function::unusable,boost::detail::function::unusable,boost::detail::function::unusable,boost::detail::function::unusable,boost::detail::function::unusable,boost::detail::function::unusable,boost::detail::function::unusable>::function<boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > > (boost::_bi::bind_t<int,boost::_mfi::mf2<int,B,const D *,int>,boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> > >)' testBoost.C:61: instantiated from here /usr/local/devl/src/incl/boost/bind.hpp:263: no match for call to `(boost::_mfi::mf2<int,B,const D *,int>) (B *const &, int &, D *const &)' /usr/local/devl/src/incl/boost/bind/mem_fn_template.hpp:212: candidates are: int boost::_mfi::mf2<int,B,const D *,int>::operator ()(B *, const D *, int) const /usr/local/devl/src/incl/boost/bind/mem_fn_template.hpp:217: int boost::_mfi::mf2<int,B,const D *,int>::operator ()<B *const>(B *const &, const D *, int) const /usr/local/devl/src/incl/boost/bind/mem_fn_template.hpp:222: int boost::_mfi::mf2<int,B,const D *,int>::operator ()(B &, const D *, int) const /usr/local/devl/src/incl/boost/bind.hpp:263: warning: control reaches end of non-void function `boost::_bi::list3<boost::_bi::value<B *>,boost::arg<1>,boost::_bi::value<D *> >::operator ()<int, boost::_mfi::mf2<int,B,const D *,int>, boost::_bi::list1<int &> (boost::_bi::type<int>, boost::_mfi::mf2<int,B,const D *,int>, boost::_bi::list1<int &> &) const'

Hi Bill, It's just a typo that's causing your problem: not_ok is declared like this: int not_ok(const D *d, int j) { return d->d() + j; } But the call to bind swaps the order of the 'const D*' and 'int' arguments: Foo f2 = boost::bind(&B::not_ok, b, _1, d); You want: Foo f2 = boost::bind(&B::not_ok, b, d, _1); Doug

Douglas Gregor wrote:
Hi Bill, It's just a typo that's causing your problem: not_ok is declared like this:
int not_ok(const D *d, int j) { return d->d() + j; }
But the call to bind swaps the order of the 'const D*' and 'int' arguments:
Foo f2 = boost::bind(&B::not_ok, b, _1, d);
You want:
Foo f2 = boost::bind(&B::not_ok, b, d, _1);
Doug
Doh! What I had really done was not understand what the _# args were for. Thanks, Bill

Douglas Gregor wrote:
Hi Bill, It's just a typo that's causing your problem: not_ok is declared like this:
int not_ok(const D *d, int j) { return d->d() + j; }
But the call to bind swaps the order of the 'const D*' and 'int' arguments:
Foo f2 = boost::bind(&B::not_ok, b, _1, d);
You want:
Foo f2 = boost::bind(&B::not_ok, b, d, _1);
Doh! It's not a typo, it's a major misunderstanding of the role of the _1. *Now* I get it.... Thanks!
participants (2)
-
Bill Dozier
-
Douglas Gregor