[boost::bind] How use with fn objects?
The documentation gives examples for
int operator()(int a, int b)...
bool operator()(long a, long b)...
that use _1 to represent the first parameter (i.e., 'a').
I'm trying to pass a function object that takes no
parameters to a boost::thread constructor and cannot
get it right.
BRIDGES Dick wrote:
The documentation gives examples for int operator()(int a, int b)... bool operator()(long a, long b)... that use _1 to represent the first parameter (i.e., 'a').
I'm trying to pass a function object that takes no parameters to a boost::thread constructor and cannot get it right.
class Foo { void operator()() { /* do something */ return; } } int main( int argc, char **argv ) {
Foo f; boost::thread doit( boost:bind<void>(f,_1)() ); doit.join();
return 0; }
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
BRIDGES Dick wrote:
class Foo { void operator()() { /* do something */ return; } }
int main( int argc, char **argv ) {
Foo f; boost::thread doit( boost:bind<void>(f,_1)() ); doit.join();
return 0; }
Isn't the '_1' required for the this pointer? If it's not required, what is the correct form?
In this specific case you don't need boost::bind at all. boost::thread doit( f ); If you had class Foo { void operator()( int x ) { /* do something with x */ return; } } then you'd need to use bind to supply a value for x: boost::thread doit( boost:bind<void>( f, 5 ) );
participants (3)
-
BRIDGES Dick
-
Peter Dimov
-
Rene Rivera