Peter Bingham Peter Bingham -4 years ago 111
C++ Question

How to store the buffer for boost::asio::write as a shared_ptr?

it states for the buffers that "ownership of the underlying memory blocks is retained by the caller, which must guarantee that they remain valid until the handler is called."

In order to do this I am first creating a shared_ptr to the buffer via:

request_buff_ = std::make_shared<boost::asio::const_buffer> (boost::asio::buffer(request_str));

where request_str is an std::string. Then dereference before writing:

boost::asio::write(socket, *request_buff_);

Get the following compile error:

'const_iterator': is not a member of 'boost::asio::const_buffer'

Answer Source

The buffer is a simple reference (pointer+size tuple). It can be cheaply copied by value. Instead, make the underlying storage shared, so e.g.

auto s = std::make_shared<std::string>("payload");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download