In the example of an asynchronous tcp server, a function is associated at boost (all this happens in a class that inherits enable_shared_from_this ):

 boost::asio::async_write(socket_, boost::asio::buffer(message_), boost::bind(&tcp_connection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); ... void handle_write(const boost::system::error_code& /*error*/, size_t /*bytes_transferred*/) { } 

Why when calling boost::bind(...) in its arguments is passed to shared_from_this() ? What's the point if all the arguments to boost::bind(...) (except the first) are to be used as arguments to handle_write(...) , but are there no pointer to classes in the arguments to handle_write(...) ?

    1 answer 1

    handle_write is a member function, i.e. the first (implicit) parameter takes the this pointer. This this , though already in the form of shared_ptr , rather than the usual raw pointer, is associated with bind with handle_write .

    Thus, we can say that the async_write call is executed as async_write :

     shared_from_this()->handle_write( /*остальные параметры*/); 

    The presence of shared_ptr seems to be required due to an asynchronous call, i.e. when the function can actually "survive" the object, which it takes as a parameter through the pointer.