Handler requirement for Asynchronous Operations in boost::asio
It is specified in http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/AcceptHan... that a handler for `async_accept()` must satisfy the following function signature: void accept_handler( const boost::system::error_code& ec) { ... } However, in the `Daytime.3` example, using boost::bind, the handler can be specified as much parameter as desired, as long as it does not exceed the limit of `boost::bind` (which is 9 arguments at maximum): class tcp_server { public: tcp_server(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13)) { start_accept(); } private: void start_accept() { tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.get_io_service()); acceptor_.async_accept(new_connection->socket(), boost::bind(&tcp_server::handle_accept, this, new_connection, boost::asio::placeholders::error)); } void handle_accept(tcp_connection::pointer new_connection, const boost::system::error_code& error) { if (!error) { new_connection->start(); } start_accept(); } tcp::acceptor acceptor_; }; Why is that possible? I thought even with `boost::bind`, the exact function signature still has to be matched. Notice the `handle_accept()` funciton and how it is used in `async_accept()`. The full code listing is here: http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/tutorial/tutdaytime...
It is specified in http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/AcceptHan... that a handler for `async_accept()` must satisfy the following function signature:
void accept_handler( const boost::system::error_code& ec) { ... }
However, in the `Daytime.3` example, using boost::bind, the handler can be specified as much parameter as desired, as long as it does not exceed the limit of `boost::bind` (which is 9 arguments at maximum): <...> Why is that possible? I thought even with `boost::bind`, the exact function signature still has to be matched.
It is possible, because bind creates a functor that exposes operator() having the number of arguments that matches the expectations of the completion handler caller.
I found a very good explanation here:
http://blog.think-async.com/2010/04/bind-illustrated.html
On Tue, Dec 20, 2011 at 12:07 AM, Igor R
It is specified in http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/AcceptHan... that a handler for `async_accept()` must satisfy the following function signature:
void accept_handler( const boost::system::error_code& ec) { ... }
However, in the `Daytime.3` example, using boost::bind, the handler can be specified as much parameter as desired, as long as it does not exceed the limit of `boost::bind` (which is 9 arguments at maximum): <...> Why is that possible? I thought even with `boost::bind`, the exact function signature still has to be matched.
It is possible, because bind creates a functor that exposes operator() having the number of arguments that matches the expectations of the completion handler caller. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Igor R
-
Tu Hoang Do