Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boost::span rvalue is not supported by asio::buffers #438

Open
ujos opened this issue Apr 29, 2024 · 1 comment
Open

boost::span rvalue is not supported by asio::buffers #438

ujos opened this issue Apr 29, 2024 · 1 comment

Comments

@ujos
Copy link

ujos commented Apr 29, 2024

I have a function that returns a buffer as a span to read the data from socket:

boost::span<std::byte> getRecvBuffer();

When I call tcp::socket::async_receive(asio::buffer(getRecvBuffer())),

socket_.async_receive(asio::buffer(getRecvBuffer()), [](auto&&...) {});

... I get an error:

error: no matching function for call to ‘boost::asio::mutable_buffer::mutable_buffer(const boost::asio::const_buffers_1&)’

The weird thing is that following code compiles:

auto b1 = asio::buffer(getRecvBuffer());

Following code compiles too:

auto buf = client.socketRecvBuffer_.recvBuffer();
socket_.async_receive(asio::buffer(buf), [](auto&&...) {});

It seems in order to create a mutable_buffer, I need a lvalue for the buffer, which makes sense in case of std::vector, but it does not make sense for boost::span.

@sbhutch
Copy link

sbhutch commented Nov 13, 2024

I believe I have come across a related issue (Boost 1.83) where the following code does not work:

std::array buffers {
    boost::asio::buffer(packet1.getPayload()),
    boost::asio::buffer(packet2.getPayload()),
};

boost::asio::read(socket, buffers);

Where the Packet::getPayload signature is:

class Packet {
public:
    boost::span<unsigned char> getPayload();
};

Type deduction on the std::array is:

std::array<boost::asio::const_buffers_1, 2UL> buffers

The boost::asio::read function fails due to the const_buffers_1 deduction. However, if I provide the spans as an lvalue:

auto buffer1 = packet1.getPayload();
auto buffer2 = packet2.getPayload();

std::array buffers {
    boost::asio::buffer(buffer1),
    boost::asio::buffer(buffer2),
};

boost::asio::read(socket, buffers);

Type deduction on the std::array becomes:

std::array<boost::asio::mutable_buffers_1, 2UL> buffers

and all works well.

@mclow mclow transferred this issue from boostorg/boost Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants