std::basic_spanstream<CharT,Traits>::span
From cppreference.com
< cpp | io | basic spanstream
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
std::span <CharT> span() const noexcept;
(1)
(since C++23)
void span( std::span <CharT> s ) noexcept;
(2)
(since C++23)
1) Gets a
span
referencing the written area if std::ios_base::out is set in the open mode of the wrapped std::basic_spanbuf, or a span
referencing the underlying buffer otherwise.2) Makes the wrapped std::basic_spanbuf perform I/O on the buffer referenced by s.
Contents
[edit] Parameters
s
-
std::span referencing the storage to be use as the new underlying buffer of stream
[edit] 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) (none)
[edit] Example
Run this code
#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]