给出以下课程
class task_counter
{
public:
task_counter(short, boost::asio::io_service&);
~task_counter(void);
template<typename CompletionHandler>
void exec_task(CompletionHandler handler)
{
grant_access();
io_.post(boost::bind(&task_counter::exec_and_decrease_counter<CompletionHandler>, this, handler));
}
template<typename CompletionHandler>
void exec_and_decrease_counter(CompletionHandler handler)
{
handler();
decrease_counter();
}
private:
....
}
方法exec_task以这种方式由另一个类调用:
tc_msg->exec_task(boost::bind(&message_receiver::handle_msg, this, msg));
编译无法在bind.hpp中声明“无效使用void表达式” 我正在弄清楚问题应该在io_post函数中,其参数是两个不同的boost :: bind对象的组合。但我无法深入解决真正的问题。
答案 0 :(得分:3)
而不是:
io_.post(boost::bind(&task_counter::exec_and_decrease_counter<CompletionHandler>, this, handler));
尝试以下方法:
#include <boost/bind/protect.hpp>
//...
io_.post(boost::bind(&task_counter::exec_and_decrease_counter<boost::_bi::protected_bind_t<CompletionHandler> >, this, boost::protect(handler)));