31 Input/output library [input.output]

31.9 Span-based streams [span.streams]

31.9.1 Overview [span.streams.overview]

The header <spanstream> defines class templates and types that associate stream buffers with objects whose types are specializations of span as described in [views.span].
[Note 1: 
A user of these classes is responsible for ensuring that the character sequence represented by the given span outlives the use of the sequence by objects of the classes in subclause [span.streams].
Using multiple basic_spanbuf objects referring to overlapping underlying sequences from different threads, where at least one basic_spanbuf object is used for writing to the sequence, results in a data race.
— end note]

31.9.2 Header <spanstream> synopsis [spanstream.syn]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_spanbuf; template<class charT, class traits> void swap(basic_spanbuf<charT, traits>& x, basic_spanbuf<charT, traits>& y); using spanbuf = basic_spanbuf<char>; using wspanbuf = basic_spanbuf<wchar_t>; template<class charT, class traits = char_traits<charT>> class basic_ispanstream; template<class charT, class traits> void swap(basic_ispanstream<charT, traits>& x, basic_ispanstream<charT, traits>& y); using ispanstream = basic_ispanstream<char>; using wispanstream = basic_ispanstream<wchar_t>; template<class charT, class traits = char_traits<charT>> class basic_ospanstream; template<class charT, class traits> void swap(basic_ospanstream<charT, traits>& x, basic_ospanstream<charT, traits>& y); using ospanstream = basic_ospanstream<char>; using wospanstream = basic_ospanstream<wchar_t>; template<class charT, class traits = char_traits<charT>> class basic_spanstream; template<class charT, class traits> void swap(basic_spanstream<charT, traits>& x, basic_spanstream<charT, traits>& y); using spanstream = basic_spanstream<char>; using wspanstream = basic_spanstream<wchar_t>; }

31.9.3 Class template basic_spanbuf [spanbuf]

31.9.3.1 General [spanbuf.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_spanbuf : public basic_streambuf<charT, traits> { public: using char_type = charT; using int_type = typename traits::int_type; using pos_type = typename traits::pos_type; using off_type = typename traits::off_type; using traits_type = traits; // [spanbuf.cons], constructors basic_spanbuf() : basic_spanbuf(ios_base::in | ios_base::out) {} explicit basic_spanbuf(ios_base::openmode which) : basic_spanbuf(std::span<charT>(), which) {} explicit basic_spanbuf(std::span<charT> s, ios_base::openmode which = ios_base::in | ios_base::out); basic_spanbuf(const basic_spanbuf&) = delete; basic_spanbuf(basic_spanbuf&& rhs); // [spanbuf.assign], assignment and swap basic_spanbuf& operator=(const basic_spanbuf&) = delete; basic_spanbuf& operator=(basic_spanbuf&& rhs); void swap(basic_spanbuf& rhs); // [spanbuf.members], member functions std::span<charT> span() const noexcept; void span(std::span<charT> s) noexcept; protected: // [spanbuf.virtuals], overridden virtual functions basic_streambuf<charT, traits>* setbuf(charT*, streamsize) override; pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out) override; pos_type seekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out) override; private: ios_base::openmode mode; // exposition only std::span<charT> buf; // exposition only }; }
The class template basic_spanbuf is derived from basic_streambuf to associate possibly the input sequence and possibly the output sequence with a sequence of arbitrary characters.
The sequence is provided by an object of class span<charT>.
For the sake of exposition, the maintained data is presented here as:
  • ios_base​::​openmode mode, has in set if the input sequence can be read, and out set if the output sequence can be written.
  • std​::​span<charT> buf is the view to the underlying character sequence.

31.9.3.2 Constructors [spanbuf.cons]

explicit basic_spanbuf(std::span<charT> s, ios_base::openmode which = ios_base::in | ios_base::out);
Effects: Initializes the base class with basic_streambuf() ([streambuf.cons]), and mode with which.
Initializes the internal pointers as if calling span(s).
basic_spanbuf(basic_spanbuf&& rhs);
Effects: Initializes the base class with std​::​move(rhs) and mode with std​::​move(rhs.mode) and buf with std​::​move(rhs.buf).
The sequence pointers in *this (eback(), gptr(), egptr(), pbase(), pptr(), epptr()) obtain the values which rhs had.
It is implementation-defined whether rhs.buf.​empty() returns true after the move.
Postconditions: Let rhs_p refer to the state of rhs just prior to this construction.
  • span().data() == rhs_p.span().data()
  • span().size() == rhs_p.span().size()
  • eback() == rhs_p.eback()
  • gptr() == rhs_p.gptr()
  • egptr() == rhs_p.egptr()
  • pbase() == rhs_p.pbase()
  • pptr() == rhs_p.pptr()
  • epptr() == rhs_p.epptr()
  • getloc() == rhs_p.getloc()

31.9.3.3 Assignment and swap [spanbuf.assign]

basic_spanbuf& operator=(basic_spanbuf&& rhs);
Effects: Equivalent to: basic_spanbuf tmp{std::move(rhs)}; this->swap(tmp); return *this;
void swap(basic_spanbuf& rhs);
Effects: Equivalent to: basic_streambuf<charT, traits>::swap(rhs); std::swap(mode, rhs.mode); std::swap(buf, rhs.buf);
template<class charT, class traits> void swap(basic_spanbuf<charT, traits>& x, basic_spanbuf<charT, traits>& y);
Effects: Equivalent to x.swap(y).

31.9.3.4 Member functions [spanbuf.members]

std::span<charT> span() const noexcept;
Returns: If ios_base​::​out is set in mode, returns std​::​span<charT>(pbase(), pptr()), otherwise returns buf.
[Note 1: 
In contrast to basic_stringbuf, the underlying sequence never grows and is not owned.
An owning copy can be obtained by converting the result to basic_string<charT>.
— end note]
void span(std::span<charT> s) noexcept;
Effects: buf = s.
Initializes the input and output sequences according to mode.
Postconditions:
  • If ios_base​::​out is set in mode, pbase() == s.data() && epptr() == pbase() + s.size() is true;
    • in addition, if ios_base​::​ate is set in mode, pptr() == pbase() + s.size() is true,
    • otherwise pptr() == pbase() is true.
  • If ios_base​::​in is set in mode, eback() == s.data() && gptr() == eback() && egptr() == eback() + s.size() is true.

31.9.3.5 Overridden virtual functions [spanbuf.virtuals]

[Note 1: 
Because the underlying buffer is of fixed size, neither overflow, underflow, nor pbackfail can provide useful behavior.
— end note]
pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out) override;
Effects: Alters the stream position within one or both of the controlled sequences, if possible, as follows:
  • If ios_base​::​in is set in which, positions the input sequence; xnext is gptr(), xbeg is eback().
  • If ios_base​::​out is set in which, positions the output sequence; xnext is pptr(), xbeg is pbase().
If both ios_base​::​in and ios_base​::​out are set in which and way is ios_base​::​cur, the positioning operation fails.
For a sequence to be positioned, if its next pointer xnext (either gptr() or pptr()) is a null pointer and the new offset newoff as computed below is nonzero, the positioning operation fails.
Otherwise, the function determines baseoff as a value of type off_type as follows:
  • 0 when way is ios_base​::​beg;
  • (pptr() - pbase()) for the output sequence, or (gptr() - eback()) for the input sequence when way is ios_base​::​cur;
  • when way is ios_base​::​end :
    • (pptr() - pbase()) if ios_base​::​out is set in mode and ios_base​::​in is not set in mode,
    • buf.size() otherwise.
If would overflow, or if is less than zero, or if is greater than buf.size(), the positioning operation fails.
Otherwise, the function computes off_type newoff = baseoff + off; and assigns xbeg + newoff to the next pointer xnext.
Returns: pos_type(off_type(-1)) if the positioning operation fails; pos_type(newoff) otherwise.
pos_type seekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out) override;
Effects: Equivalent to: return seekoff(off_type(sp), ios_base::beg, which);
basic_streambuf<charT, traits>* setbuf(charT* s, streamsize n) override;
Effects: Equivalent to: this->span(std::span<charT>(s, n)); return this;

31.9.4 Class template basic_ispanstream [ispanstream]

31.9.4.1 General [ispanstream.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_ispanstream : public basic_istream<charT, traits> { public: using char_type = charT; using int_type = typename traits::int_type; using pos_type = typename traits::pos_type; using off_type = typename traits::off_type; using traits_type = traits; // [ispanstream.cons], constructors explicit basic_ispanstream(std::span<charT> s, ios_base::openmode which = ios_base::in); basic_ispanstream(const basic_ispanstream&) = delete; basic_ispanstream(basic_ispanstream&& rhs); template<class ROS> explicit basic_ispanstream(ROS&& s); basic_ispanstream& operator=(const basic_ispanstream&) = delete; basic_ispanstream& operator=(basic_ispanstream&& rhs); // [ispanstream.swap], swap void swap(basic_ispanstream& rhs); // [ispanstream.members], member functions basic_spanbuf<charT, traits>* rdbuf() const noexcept; std::span<const charT> span() const noexcept; void span(std::span<charT> s) noexcept; template<class ROS> void span(ROS&& s) noexcept; private: basic_spanbuf<charT, traits> sb; // exposition only }; }
[Note 1: 
Constructing an ispanstream from a string-literal includes the termination character '\0' in the underlying spanbuf.
— end note]

31.9.4.2 Constructors [ispanstream.cons]

explicit basic_ispanstream(std::span<charT> s, ios_base::openmode which = ios_base::in);
Effects: Initializes the base class with basic_istream<charT, traits>(addressof(sb)) and sb with basic_spanbuf<charT, traits>(s, which | ios_base​::​in) ([spanbuf.cons]).
basic_ispanstream(basic_ispanstream&& rhs);
Effects: Initializes the base class with std​::​move(rhs) and sb with std​::​move(rhs.sb).
Next, basic_istream<charT, traits>​::​set_rdbuf(addressof(sb)) is called to install the contained basic_spanbuf.
template<class ROS> explicit basic_ispanstream(ROS&& s)
Constraints: ROS models ranges​::​borrowed_range.
!convertible_to<ROS, std​::​span<charT>> && convertible_to<ROS, std​::​span<charT const>> is true.
Effects: Let sp be std​::​span<const charT>(std​::​forward<ROS>(s)).
Equivalent to basic_ispanstream(std::span<charT>(const_cast<charT*>(sp.data()), sp.size()))

31.9.4.3 Swap [ispanstream.swap]

void swap(basic_ispanstream& rhs);
Effects: Equivalent to: basic_istream<charT, traits>::swap(rhs); sb.swap(rhs.sb);
template<class charT, class traits> void swap(basic_ispanstream<charT, traits>& x, basic_ispanstream<charT, traits>& y);
Effects: Equivalent to x.swap(y).

31.9.4.4 Member functions [ispanstream.members]

basic_spanbuf<charT, traits>* rdbuf() const noexcept;
Effects: Equivalent to: return const_cast<basic_spanbuf<charT, traits>*>(addressof(sb));
std::span<const charT> span() const noexcept;
Effects: Equivalent to: return rdbuf()->span();
void span(std::span<charT> s) noexcept;
Effects: Equivalent to rdbuf()->span(s).
template<class ROS> void span(ROS&& s) noexcept;
Constraints: ROS models ranges​::​borrowed_range.
(!convertible_to<ROS, std​::​span<charT>>) && convertible_to<ROS, std​::​span<const charT>> is true.
Effects: Let sp be std​::​span<const charT>(std​::​forward<ROS>(s)).
Equivalent to: this->span(std::span<charT>(const_cast<charT*>(sp.data()), sp.size()))

31.9.5 Class template basic_ospanstream [ospanstream]

31.9.5.1 General [ospanstream.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_ospanstream : public basic_ostream<charT, traits> { public: using char_type = charT; using int_type = typename traits::int_type; using pos_type = typename traits::pos_type; using off_type = typename traits::off_type; using traits_type = traits; // [ospanstream.cons], constructors explicit basic_ospanstream(std::span<charT> s, ios_base::openmode which = ios_base::out); basic_ospanstream(const basic_ospanstream&) = delete; basic_ospanstream(basic_ospanstream&& rhs); basic_ospanstream& operator=(const basic_ospanstream&) = delete; basic_ospanstream& operator=(basic_ospanstream&& rhs); // [ospanstream.swap], swap void swap(basic_ospanstream& rhs); // [ospanstream.members], member functions basic_spanbuf<charT, traits>* rdbuf() const noexcept; std::span<charT> span() const noexcept; void span(std::span<charT> s) noexcept; private: basic_spanbuf<charT, traits> sb; // exposition only }; }

31.9.5.2 Constructors [ospanstream.cons]

explicit basic_ospanstream(std::span<charT> s, ios_base::openmode which = ios_base::out);
Effects: Initializes the base class with basic_ostream<charT, traits>(addressof(sb)) and sb with basic_spanbuf<charT, traits>(s, which | ios_base​::​out) ([spanbuf.cons]).
basic_ospanstream(basic_ospanstream&& rhs) noexcept;
Effects: Initializes the base class with std​::​move(rhs) and sb with std​::​move(rhs.sb).
Next, basic_ostream<charT, traits>​::​set_rdbuf(addressof(sb)) is called to install the contained basic_spanbuf.

31.9.5.3 Swap [ospanstream.swap]

void swap(basic_ospanstream& rhs);
Effects: Equivalent to: basic_ostream<charT, traits>::swap(rhs); sb.swap(rhs.sb);
template<class charT, class traits> void swap(basic_ospanstream<charT, traits>& x, basic_ospanstream<charT, traits>& y);
Effects: Equivalent to x.swap(y).

31.9.5.4 Member functions [ospanstream.members]

basic_spanbuf<charT, traits>* rdbuf() const noexcept;
Effects: Equivalent to: return const_cast<basic_spanbuf<charT, traits>*>(addressof(sb));
std::span<charT> span() const noexcept;
Effects: Equivalent to: return rdbuf()->span();
void span(std::span<charT> s) noexcept;
Effects: Equivalent to rdbuf()->span(s).

31.9.6 Class template basic_spanstream [spanstream]

31.9.6.1 General [spanstream.general]

namespace std { template<class charT, class traits = char_traits<charT>> class basic_spanstream : public basic_iostream<charT, traits> { public: using char_type = charT; using int_type = typename traits::int_type; using pos_type = typename traits::pos_type; using off_type = typename traits::off_type; using traits_type = traits; // [spanstream.cons], constructors explicit basic_spanstream(std::span<charT> s, ios_base::openmode which = ios_base::out | ios_base::in); basic_spanstream(const basic_spanstream&) = delete; basic_spanstream(basic_spanstream&& rhs); basic_spanstream& operator=(const basic_spanstream&) = delete; basic_spanstream& operator=(basic_spanstream&& rhs); // [spanstream.swap], swap void swap(basic_spanstream& rhs); // [spanstream.members], members basic_spanbuf<charT, traits>* rdbuf() const noexcept; std::span<charT> span() const noexcept; void span(std::span<charT> s) noexcept; private: basic_spanbuf<charT, traits> sb; // exposition only }; }

31.9.6.2 Constructors [spanstream.cons]

explicit basic_spanstream(std::span<charT> s, ios_base::openmode which = ios_base::out | ios_bas::in);
Effects: Initializes the base class with basic_iostream<charT, traits>(addressof(sb)) and sb with basic_spanbuf<charT, traits>(s, which) ([spanbuf.cons]).
basic_spanstream(basic_spanstream&& rhs);
Effects: Initializes the base class with std​::​move(rhs) and sb with std​::​move(rhs.sb).
Next, basic_iostream<charT, traits>​::​set_rdbuf(addressof(sb)) is called to install the contained basic_spanbuf.

31.9.6.3 Swap [spanstream.swap]

void swap(basic_spanstream& rhs);
Effects: Equivalent to: basic_iostream<charT, traits>::swap(rhs); sb.swap(rhs.sb);
template<class charT, class traits> void swap(basic_spanstream<charT, traits>& x, basic_spanstream<charT, traits>& y);
Effects: Equivalent to x.swap(y).

31.9.6.4 Member functions [spanstream.members]

basic_spanbuf<charT, traits>* rdbuf() const noexcept;
Effects: Equivalent to: return const_cast<basic_spanbuf<charT, traits>*>(addressof(sb));
std::span<charT> span() const noexcept;
Effects: Equivalent to: return rdbuf()->span();
void span(std::span<charT> s) noexcept;
Effects: Equivalent to rdbuf()->span(s).