std::basic_...tream::span(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ std::basic_...tream::span(3) ✨

okay so we’re in the C++ Standard Libary manual, bestie β€” yes, that spelling is literally on the page, don’t come for me πŸ’€. This one is all about std::basic_ispanstream::span, and the page title is std::basic_...tream::span(3), so we’re keeping both exactly as printed 🫢.


🌸 NAME 🌸

std::basic_ispanstream::span β€” std::basic_ispanstream::span


πŸ¦‹ Synopsis πŸ¦‹

std::span<const CharT> span() const noexcept; (1) (since C++23)
void span( std::span<CharT> s ) noexcept;     (2) (since C++23)
template< class SpanLike >                    (3) (since C++23)
void span( SpanLike&& r ) noexcept;
  1. Gets a std::span referencing the written area if std::ios_base::out is set in the open mode of the wrapped std::basic_spanbuf, or a std::span referencing the underlying buffer otherwise. it’s giving β€œdepends on the mode” energy πŸ’…
  2. Makes the wrapped std::basic_spanbuf perform I/O on the buffer referenced by s. iconic little rebuffer move ✨
  3. Same as (2), except that s is obtained as if by:
std::span<const CharT> cs{std::forward<SpanLike>(r)};
std::span<CharT> s{const_cast<CharT*>(cs.data()), cs.size()};

This overload participates in overload resolution only if SpanLike models borrowed_range, std::convertible_to<SpanLike, std::span<CharT>> is false, and std::convertible_to<SpanLike, std::span<const CharT>> is true. very β€œonly if the vibes are exactly right” of it πŸŽ€


πŸŽ€ Parameters πŸŽ€

  • s β€” std::span referencing the storage to be used as the new underlying buffer of stream.
  • r β€” borrowed_range to be used as the new underlying buffer of stream.

πŸ’– Return value πŸ’–

  1. A std::span referencing the underlying buffer or written area, depending on the open mode of the wrapped std::basic_spanbuf. 2,3) (none)

🌈 Example 🌈

Run this code, bestie β€” it’s a whole little spanstream story:

#include <cassert>
#include <iostream>
#include <span>
#include <spanstream>

int main()
{
    char out_buf[16];
    std::ospanstream ost{std::span<char>{out_buf}};
    ost << "C++" << ' ' << 23 << '\0'; // note explicit null-termination
    auto sp = ost.span();
    assert(
        sp[0] == 'C' && sp[1] == '+' && sp[2] == '+' &&
        sp[3] == ' ' && sp[4] == '2' && sp[5] == '3' &&
        sp[6] == '\0'
    );
    std::cout << "sp.data(): [" << sp.data() << "]\n";
    std::cout << "out_buf: [" << out_buf << "]\n";
    // spanstream uses out_buf as internal storage, no allocations
    assert(static_cast<char*>(out_buf) == sp.data());

    const char in_buf[] = "X Y 42";
    std::ispanstream ist{std::span<const char>{in_buf}};
    assert(static_cast<const char*>(in_buf) == ist.span().data());
    char c;
    ist >> c;
    assert(c == 'X');
    ist >> c;
    assert(c == 'Y');
    int i;
    ist >> i;
    assert(i == 42);
    ist >> i; // buffer is exhausted
    assert(!ist);
}

🌟 Output: 🌟

sp.data(): [C++ 23]
out_buf: [C++ 23]

πŸ‘€ See also πŸ‘€

  • span β€” obtains or initializes an underlying buffer according to mode; (C++23) public member function of std::basic_spanbuf<CharT,Traits>

🫢 footer vibes, exactly as the page closes: http://cppreference.com, 2024.06.10, std::basic_...tream::span(3). That’s the source/version/date trio, bestie β€” periodt πŸ’…
πŸ“¦ where this page comes from