
On 29/03/2017 03:28, Christopher Pisz via Boost-users wrote:
If I followed what you wrote in your post correctly, I would end up with:
void start_accept() { tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.get_io_service());
//auto callback = std::bind(&tcp_server::handle_accept, this, new_connection, std::placeholders::_1); const std::function<void(tcp_connection::pointer, const boost::system::error_code &)> callback = std::bind(&tcp_server::handle_accept, this, new_connection, std::placeholders::_1);
acceptor_.async_accept(new_connection->socket(), std::bind(callback, this, boost::asio::placeholders::error)); }
Which does not compile. I don't know why I'd bind something that is already bound or what this would mean in the context of the second bind.
All that bind does is to return a functor that has fewer parameters than another functor, by "binding" some specific values to some parameters while letting others pass through from the caller. You can bind as many times as you like (although there's a slight performance hit due to the indirection). (Though in this context there's no point in doing two binds -- it's always possible to write that as a single bind, or as a lambda. You'd only bind multiple times if it needed to happen in different contexts.) Now, have a look at the code you wrote: const std::function<void(tcp_connection::pointer, const boost::system::error_code&)> callback = std::bind(&tcp_server::handle_accept, this, new_connection, std::placeholders::_1); You are declaring a function that takes a tcp_connection::pointer and a const boost::system::error_code& as parameters -- that's two parameters. But you're only using one placeholder in the bind, which means that the second parameter doesn't go anywhere, which is probably not your intention. Meanwhile in the second bind you're trying to mix boost binding with std binding. Either use boost::bind for the async_accept call or use std::placeholders::_1 instead of the boost::asio::placeholders::error. Also, "this" is probably not a tcp_connection::pointer, so it's not valid to try to bind that to the first parameter of callback. (You can mix binds to a certain extent -- a boost bind can be stored in a std::function and vice versa, and you can boost bind a std::function and vice versa, but you can't use std:: placeholders in a boost:: bind and vice versa.) In any case, this is completely different code from what you had in your previous post. I already showed you what to use for your previous code.