Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost::function] Is there a way to assign a generic function, regardless number of parameters?
From: Igor R (boost.lists_at_[hidden])
Date: 2010-01-28 11:22:23


> void handler1( );
> void handler2(unsigned int i1, unsigned int i2, float f, char* c);
>
> and pass both handlers to an async function of asio and have no problem.

How can you pass both handlers to an async function of asio?
Maybe, you can create a "binder" based on these functions, but the
resulting functor should have the number of arguments required by asio
concepts.

> /* some code doing stuff ... */
>           schedule_callback(boost::bind(callback1, parameter1, parameter2));
> /* ... do some more things ... */
> schedule_callback(boost::bind(&FooClass::method1, FooInstancePointer,
> param1));
> /* more code  ... */
> schedule_callback(boost::bind(callback2, param1));

Actually, the question is: *who* will set parameter1, parameter2 etc?
If you know the values of these arguments in the place where you call
schedule_callback, and the callback invoker should not set pass any
arguments to your handler, then the prototype of your handler should
be: void(void). Then you can do something like this:

void shedule_callback(function<void(void>);
void handler1(int, int, std::string);
//...
schedule_callback(bind(&handler1, 5, 7, "hello"));

But if the callback inoker should pass some parameters to your
handler, the handler prototype should reflect this fact.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net