boost::bind as an argument to accept function with n args and use the same on further function calls

32 Views Asked by At

Can i do something like below ? Is it possible or is there any workaround?

..
PostWorkToThread( boost::bind(func_x, arg1) );
PostWorkToThread( boost::bind(func_y, arg1, arg2) );
PostWorkToThread( boost::bind(func_z, arg1, arg2, arg3) );
..


void PostWorkToThread( boost::bind xxx )
{
    PostWork( boost::bind(xxx) ); or
    PostWork( xxx );
}

Thank you, I appreciate your suggestion.

1

There are 1 best solutions below

0
Andrey Semashev On

Since the type of the function object generated by boost::bind (and std::bind) is not specified, you should make PostWorkToThread a template:

template< typename Fun >
void PostWorkToThread( Fun xxx )
{
    // Enqueue xxx for execution
}

Alternatively, you could use boost::function (or std::function) to erase the type of the function object:

void PostWorkToThread( boost::function< void() > const& xxx )
{
    // Enqueue xxx for execution
}

Note that in this case boost::function (and std::function) may have to dynamically allocate memory to store the function object. However, you will likely have to do it anyway to enqueue the function for execution, so this is probably not a problem.