boost::bind 作为参数接受具有 n 个参数的函数,并在进一步的函数调用中使用相同的参数



我可以做下面这样的事情吗?是否有可能或有任何解决方法?

..
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 );
}

谢谢,感谢您的建议。

由于未指定由boost::bind(和std::bind(生成的函数对象的类型,因此应将PostWorkToThread创建为模板:

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

或者,您可以使用boost::function(或std::function(擦除函数对象的类型:

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

请注意,在这种情况下,boost::function(和std::function(可能必须动态分配内存来存储函数对象。但是,您可能无论如何都必须这样做才能将函数排队执行,因此这可能不是问题。

相关内容

最新更新