【问题标题】:boost::bind composition inside io_service::post functionio_service::post 函数内的 boost::bind 组合
【发布时间】:2012-02-21 17:57:28
【问题描述】:

给定以下类

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 对象的组合。但我无法深入解决真正的问题。

【问题讨论】:

  • message_receiver::handle_msg的定义是什么?
  • 嗨,这是一个带有这个签名的方法:`void message_receiver::handle_msg(const qpid::client::Message& msg)`

标签: c++ boost bind boost-asio boost-bind


【解决方案1】:

代替:

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

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 2012-11-26
    • 2011-09-16
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多