Search code examples
c++boostbindboost-asioboost-bind

boost::bind composition inside io_service::post function


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.


Solution

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