Re: [Boost-users] [boost::bind] How use with fn objects?

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Rene Rivera Sent: Monday, August 29, 2005 12:30 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [boost::bind] How use with fn objects?
BRIDGES Dick wrote: [snip]
I'm trying to pass a function object that takes no parameters to a boost::thread constructor and cannot get it right. [snip]
Either:
int main( int argc, char **argv ) { Foo f; boost::thread doit( boost:bind(Foo::operator(),&f)() ); doit.join(); return 0; }
Or:
int main( int argc, char **argv ) { Foo f; boost::thread doit( f ); doit.join(); return 0; }
Should work.
-- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - Grafik/jabber.org
After I figured out I had to use boost::ref(f) with my real class, the second form worked fine. I had a problem with the first form though. <code> boost::thread doit( boost::bind(Foo::operator(),&f)() ); </code> Yielded the following [edited to fit] result: <result> g++ -D_REENTRANT -I/usr/local -I/usr/local/boost_1_33_0 -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -Wno-non-virtual-dtor -omain.o ../main.cpp ../main.cpp: In function `int main(int, char**)': ../main.cpp:22: error: invalid use of non-static member function `void my_namespace::Foo::operator()()' /usr/local/boost_1_33_0/boost/bind.hpp: At global scope: /usr/local/boost_1_33_0/boost/bind.hpp: In instantiation of `boost::_bi::result_traits<boost::_bi::unspecified, void (my_namespace::Foo::)()>': /usr/local/boost_1_33_0/boost/bind/bind_template.hpp:15: instantiated from `boost::_bi::bind_t<boost::_bi::unspecified, void (my_namespace::Foo::)(), boost::_bi::list1<boost::_bi::value<my_namespace::Foo*> > >' ../main.cpp:22: instantiated from here /usr/local/boost_1_33_0/boost/bind.hpp:63: error: `void (my_namespace::Foo::)()' is not a class, struct, or union type /usr/local/boost_1_33_0/boost/bind/bind_template.hpp: In instantiation of `boost::_bi::bind_t<boost::_bi::unspecified, void (my_namespace::Foo::)(), boost::_bi::list1<boost::_bi::value<my_namespace::Foo*> > >': ../main.cpp:22: instantiated from here /usr/local/boost_1_33_0/boost/bind/bind_template.hpp:15: error: no type named `type' in `struct boost::_bi::result_traits<boost::_bi::unspecified, void (my_namespace::Foo::)()>' /usr/local/boost_1_33_0/boost/bind/bind_template.hpp:18: error: no type named `type' in `struct boost::_bi::result_traits<boost::_bi::unspecified, void (my_namespace::Foo::)()>' [snip bind_template.hpp:24 thru bind_template.hpp:138] /usr/local/boost_1_33_0/boost/bind/bind_template.hpp:143: error: no type named `type' in `struct boost::_bi::result_traits<boost::_bi::unspecified, void (my_namespace::Foo::)()>' /usr/local/boost_1_33_0/boost/bind.hpp: In instantiation of `boost::_bi::bind_t<boost::_bi::unspecified, void (my_namespace::Foo::)(), boost::_bi::list1<boost::_bi::value<my_namespace::Foo*> > >': ../main.cpp:22: instantiated from here /usr/local/boost_1_33_0/boost/bind/bind_template.hpp:160: error: field `boost::_bi::bind_t<boost::_bi::unspecified, void (my_namespace::Foo::)(), boost::_bi::list1<boost::_bi::value<my_namespace::Foo*> > >::f_' invalidly declared method type ../main.cpp: In function `int main(int, char**)': ../main.cpp:22: error: no match for call to `(boost::_bi::bind_t<boost::_bi::unspecified, void (my_namespace::Foo::)(), boost::_bi::list1<boost::_bi::value<my_namespace::Foo*> > >) ()' /usr/local/boost_1_33_0/boost/bind.hpp: In constructor `boost::_bi::bind_t<R, F, L>::bind_t(F, const L&) [with R = boost::_bi::unspecified, F = void (my_namespace::Foo::)(), L = boost::_bi::list1<boost::_bi::value<my_namespace::Foo*> >]': /usr/local/boost_1_33_0/boost/bind.hpp:1403: instantiated from `boost::_bi::bind_t<boost::_bi::unspecified, F, typename boost::_bi::list_av_1<A1>::type> boost::bind(F, A1) [with F = void (my_namespace::Foo::)(), A1 = my_namespace::Foo*]' ../main.cpp:22: instantiated from here /usr/local/boost_1_33_0/boost/bind.hpp:861: error: invalid use of non-static member function make: *** [main.o] Error 1 make: Target `all' not remade because of errors. </result> Thank you (and Peter Dimov) for your assistance. BTW I'm still unclear on *why* (f) works. It doesn't look like any pointer to member function I've read about. Isn't f.operator() treated like any other member function? Can someone suggest something to read that might help me understand? Thanks again for your help. Regards, Dick Bridges

"BRIDGES Dick" <Dick.Bridges@us.thalesgroup.com> wrote in message news:2714C5D60037C64E92E047B1903F3C9CA7E0E1@taus-ies1.dom1.taus.us.thales...
After I figured out I had to use boost::ref(f) with my real class, the second form worked fine. I had a problem with the first form though. <code> boost::thread doit( boost::bind(Foo::operator(),&f)() ); </code> Yielded the following [edited to fit] result: <result> g++ -D_REENTRANT -I/usr/local -I/usr/local/boost_1_33_0 -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -Wno-non-virtual-dtor -omain.o ../main.cpp ../main.cpp: In function `int main(int, char**)': ../main.cpp:22: error: invalid use of non-static member function `void my_namespace::Foo::operator()()' [snipped a lot] Regards, Dick Bridges
You need to write &Foo::operator() [note the address-of operator], to mean a member function pointer, while Foo::operator() means trying to call operator() as if it were a static method of the Foo class. HTH Pablo
participants (2)
-
BRIDGES Dick
-
Pablo Aguilar