29 Input/output library [input.output]

29.9 Span-based streams [span.streams]

29.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]

29.9.2 Header <spanstream> synopsis [spanstream.syn]

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

29.9.3 Class template spanbuf [spanbuf]

29.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.ctor], 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 }; template<class charT, class traits> void swap(basic_spanbuf<charT, traits>& x, basic_spanbuf<charT, traits>& y); }
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.

29.9.3.2 Constructors [spanbuf.ctor]

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()

29.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).

29.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.

29.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;

29.9.4 Class template basic_­ispanstream [ispanstream]

29.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.ctor], 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); // [ispanstream.assign], assignment and swap basic_ispanstream& operator=(const basic_ispanstream&) = delete; basic_ispanstream& operator=(basic_ispanstream&& rhs); 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 }; template<class charT, class traits> void swap(basic_ispanstream<charT, traits>& x, basic_ispanstream<charT, traits>& y); }
[Note 1:
Constructing an ispanstream from a string-literal includes the termination character '\0' in the underlying spanbuf.
— end note]

29.9.4.2 Constructors [ispanstream.ctor]

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.ctor]).
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()))

29.9.4.3 Assignment and swap [ispanstream.assign]

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).

29.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()))

29.9.5 Class template basic_­ospanstream [ospanstream]

29.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.ctor], 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); // [ospanstream.assign], assignment and swap basic_ospanstream& operator=(const basic_ospanstream&) = delete; basic_ospanstream& operator=(basic_ospanstream&& rhs); 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 }; template<class charT, class traits> void swap(basic_ospanstream<charT, traits>& x, basic_ospanstream<charT, traits>& y); }

29.9.5.2 Constructors [ospanstream.ctor]

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.ctor]).
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.

29.9.5.3 Assignment and swap [ospanstream.assign]

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).

29.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).

29.9.6 Class template basic_­spanstream [spanstream]

29.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.ctor], 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); // [spanstream.assign], assignment and swap basic_spanstream& operator=(const basic_spanstream&) = delete; basic_spanstream& operator=(basic_spanstream&& rhs); 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 }; template<class charT, class traits> void swap(basic_spanstream<charT, traits>& x, basic_spanstream<charT, traits>& y); }

29.9.6.2 Constructors [spanstream.ctor]

explicit basic_spanstream(std::span<charT> s, ios_base::openmode which = ios_base::out | ios_bas::in);
Effects: Initializes the base class with basic_­ostream<charT, traits>(addressof(sb)) and sb with basic_­spanbuf<charT, traits>(s, which) ([spanbuf.ctor]).
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.

29.9.6.3 Assignment and swap [spanstream.assign]

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).

29.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).