Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2021-09-22 14:57:26

Andrzej Krzemienski wrote:
> My observation about the example code:
> void do_read()
> {
> auto self(shared_from_this());
> *socket_ *// <-- socket passed in one place
> .async_read_some(boost::asio::buffer(data_, max_length),
> [this, *self*] // <-- socket passed in another place
> (boost::system::error_code ec, std::size_t length)
> {
> if (!ec)
> {
> do_write(length);
> }
> });
> }
> is that I have to pass my socket in two places in one function invocation.
> Whereas, at least at high level, I could imagine an interface when I only pass it
> once (by movig) to function async_read_some, and after the function has
> made the read it passes (via a move) the socket argument to the handler.

It's not that easy because you don't need just the socket, the entire session
state would have to be moved into the handler, which at minimum includes
the buffer.

Could still be possible in principle? I don't know. It's not clear if it will be a win,
because if you put the e.g. 4K buffer into the session state, it will be costly to
move, so heap-allocating it may be better (as then moving the unique_ptr is

Boost list run by bdawes at, gregod at, cpdaniel at, john at