2

The problem: I cannot compile asio to use a buffer with gsl::span

Constraints:

  • C++ Version allowed: <= C++17
  • Boost allowed: No
  • Microsoft GSL 4.0.0, or newer as it's available
  • Asio 1.28.0, or newer as it's available

I'm trying to use gsl::span in my asio-based code because it keeps track of its size. How does one construct an asio buffer given a gsl::span?

This does not compile:

#include "asio/buffer.hpp"
#include <gsl/span>
#include <array>

int main() {
    std::array<uint8_t, 12> arr{};
    auto arrBuf = asio::buffer(arr);

    gsl::span gslSpan = gsl::make_span(arr);
    auto gslSpanBuf = asio::buffer(gslSpan);
     
    return 0;
}
error: no matching function for call to ‘buffer(gsl::span<unsigned char, 18446744073709551615>&)’

This compiles, breaking out the data pointer and size of the gsl::span object.

#include "asio/buffer.hpp"
#include <gsl/span>
#include <array>

int main() {
    std::array<uint8_t, 12> arr{};
    auto arrBuf = asio::buffer(arr);

    gsl::span gslSpan = gsl::make_span(arr);
    auto gslSpanBuf = asio::buffer(gslSpan.data(), gslSpan.size());
     
    return 0;
}

Why does asio not support passing the span in as a single argument? It has both a .data() and .size().

In the docs, it states asio 1.23.0 added support:

Added `buffer()` overloads for contiguous containers, such as `const gsl::span<const uint8_t>`.
Ryan Friedman
  • 115
  • 1
  • 9
  • You code works on my machine. I am running gcc version 13.1.1, gsl version 4.0.0, and asio version 1.28. I suggest to try GSL git main branch as well. – MaxPlankton Aug 01 '23 at 17:52

0 Answers0