boost::bind composition inside io_service::post function

五迷三道 提交于 2020-01-15 03:58:41

问题


Given the following class

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:
           ....
}

Method exec_task is called by another class in this way:

 tc_msg->exec_task(boost::bind(&message_receiver::handle_msg, this, msg)); 

Compilation fails stating "invalid use of void expression" in bind.hpp I'm figuring out that the problem should be inside the io_post function whose argument is a composition of two different boost::bind objects. But I was unable to go deeply through the real problem.


回答1:


Instead of:

io_.post(boost::bind(&task_counter::exec_and_decrease_counter<CompletionHandler>, this, handler));

Try the following:

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


来源:https://stackoverflow.com/questions/9382628/boostbind-composition-inside-io-servicepost-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!