std::basic_...tream::span(3)
π NAME π
std::basic_ospanstream::span - std::basic_ospanstream::span
okay bestie, welcome to the C++ Standard Libary manual (yes, that spelling is on the page, Iβm not fixing it π). The page title is std::basic_...tream::span, and this one is all about std::basic_ospanstream::span β very C++23, very spanstream, very slay. β¨
β¨ Synopsis β¨
two overloads, both noexcept, both since C++23, no cap:
std::span<CharT> span() const noexcept; (1) (since C++23)
void span( std::span<CharT> s ) noexcept; (2) (since C++23)
- Overload (1) β gets a
std::spanreferencing the written area ifstd::ios_base::outis set in the open mode of the wrappedstd::basic_spanbuf, or astd::spanreferencing the underlying buffer otherwise. periodt. - Overload (2) β makes the wrapped
std::basic_spanbufperform I/O on the buffer referenced bys. iconic.
πΈ Parameters πΈ
sβ astd::spanreferencing the storage to be use as the new underlying buffer of stream.
π Return value π
- Overload (1) β a
std::spanreferencing the underlying buffer or written area, depending on the open mode of the wrappedstd::basic_spanbuf. - Overload (2) β none. literally void vibes π
π§ Example π§
// Run this code β yes bestie, run it:
#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>).
source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::basic_...tream::span(3) β mwah, bye bestie π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms