Re: [boost] Nested Boost Binds

Peter Dimov replied:
t.async_wait(boost::bind(Component::Method1, &y, 10, boost::bind (DPC::Print1, &x, 2), &t));
You need to prevent the inner bind from being treated as a subexpression of the outer bind. Either assign it to a function<>:
boost::function<void()> f = boost::bind( DPC::Print1, &x, 2 ); t.async_wait( boost::bind( Component::Method1, &y, 10, f ) ); or use boost::protect, as explained at the end of
I tried both suggested solutions but still have the same errors boost::function<void(int)> f = boost::bind( DPC::Print1, &x, 2 ); t.async_wait( boost::bind( Component::Method1, &y, 10, f, &t ) ); or t.async_wait( boost::bind( Component::Method1, &y, 10, boost::protect(f), &t ) ); It seems like boost::protect does not not affect this expression at all. Maybe I'm doing something wrong. Thank you in advance, Sergey

Sergey Lukin wrote:
Peter Dimov replied:
t.async_wait(boost::bind(Component::Method1, &y, 10, boost::bind (DPC::Print1, &x, 2), &t));
You need to prevent the inner bind from being treated as a subexpression of the outer bind. Either assign it to a function<>:
boost::function<void()> f = boost::bind( DPC::Print1, &x, 2 ); t.async_wait( boost::bind( Component::Method1, &y, 10, f ) ); or use boost::protect, as explained at the end of
I tried both suggested solutions but still have the same errors
boost::function<void(int)> f = boost::bind( DPC::Print1, &x, 2 ); t.async_wait( boost::bind( Component::Method1, &y, 10, f, &t ) ); or t.async_wait( boost::bind( Component::Method1, &y, 10, boost::protect(f), &t ) );
Your Component::Method1 takes two arguments, three with the implicit 'this': class Component { public: template <typename Handler> void Method1 (int x ,Handler h) { x++; h(); } }; but you are passing four, there is one extra &t at the end. You should also use & with member functions, &Component::Method1, although some compilers allow you to omit it.

Sergey Lukin wrote:
Peter Dimov replied:
t.async_wait(boost::bind(Component::Method1, &y, 10, boost::bind (DPC::Print1, &x, 2), &t));
You need to prevent the inner bind from being treated as a subexpression of the outer bind. Either assign it to a function<>:
boost::function<void()> f = boost::bind( DPC::Print1, &x, 2 ); t.async_wait( boost::bind( Component::Method1, &y, 10, f ) ); or use boost::protect, as explained at the end of
I tried both suggested solutions but still have the same errors
boost::function<void(int)> f = boost::bind( DPC::Print1, &x, 2 );
The types above are incompatible, use what Peter suggested previously: boost::function<void()> f = boost::bind( DPC::Print1, &x, 2 ); Jeff Flinn

Peter Dimov wrote:
boost::function<void(int)> f = boost::bind( DPC::Print1, &x, 2 ); t.async_wait( boost::bind( Component::Method1, &y, 10, f, &t ) ); or t.async_wait( boost::bind( Component::Method1, &y, 10, boost::protect(f), &t ) );
Your Component::Method1 takes two arguments, three with the implicit 'this':
class Component { public: template <typename Handler> void Method1 (int x ,Handler h) { x++; h(); } };
but you are passing four, there is one extra &t at the end.
Yes, you were right. I was passing not needed fifth parameter. But anyway this haven't changed error messages much. in both cases I am getting boost::bind errors ...expects 2 arguments - 4 provided ...expects 3 arguments - 4 provided ...expects 5 arguments - 4 provided ...expects 6 arguments - 4 provided ... Note: I'm not getting ... any error message for the 4 argument case, which is my case. t.async_wait( boost::bind( &Component::Method1, &y, 10, f ); t.async_wait( boost::bind( &Component::Method1, &y, 10, boost::protect(f) );
You should also use & with member functions, &Component::Method1, although some compilers allow you to omit it.
Jeff Flinn wrote:
The types above are incompatible, use what Peter suggested previously:
boost::function<void()> f = boost::bind( DPC::Print1, &x, 2 );
I tried this also - no effect whatsoever. Have you tried to compile my code ? Thank you in advance, Sergey

Sergey Lukin wrote:
Jeff Flinn wrote:
The types above are incompatible, use what Peter suggested previously:
boost::function<void()> f = boost::bind( DPC::Print1, &x, 2 );
I tried this also - no effect whatsoever.
Have you tried to compile my code ?
You are right, the code doesn't work because Component::Method1 is a template. Something like typedef boost::function< void() > handler_type; handler_type handler = boost::bind( &DPC::Print1, &x, 2 ); async_wait( boost::bind( &Component::Method1<handler_type>, &y, 10, handler ) ); should work, but doesn't, under MSVC 7.1 at least. MSVC 8.0 eats it, though. typedef boost::function< void() > handler_type; handler_type handler = boost::bind( &DPC::Print1, &x, 2 ); void (Component::*pmf) (int, handler_type) = &Component::Method1; async_wait( boost::bind( pmf, &y, 10, handler ) ); works under MSVC 7.1, but is too unwieldy.
participants (3)
-
Jeff Flinn
-
Peter Dimov
-
Sergey Lukin