29 Input/output library [input.output]

29.5 Iostreams base classes [iostreams.base]

29.5.5 Class template basic_­ios [ios]

29.5.5.1 Overview [ios.overview]

namespace std {
  template<class charT, class traits = char_traits<charT>>
  class basic_ios : public ios_base {
  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;

    // [iostate.flags], flags functions
    explicit operator bool() const;
    bool operator!() const;
    iostate rdstate() const;
    void clear(iostate state = goodbit);
    void setstate(iostate state);
    bool good() const;
    bool eof()  const;
    bool fail() const;
    bool bad()  const;

    iostate exceptions() const;
    void exceptions(iostate except);

    // [basic.ios.cons], constructor/destructor
    explicit basic_ios(basic_streambuf<charT, traits>* sb);
    virtual ~basic_ios();

    // [basic.ios.members], members
    basic_ostream<charT, traits>* tie() const;
    basic_ostream<charT, traits>* tie(basic_ostream<charT, traits>* tiestr);

    basic_streambuf<charT, traits>* rdbuf() const;
    basic_streambuf<charT, traits>* rdbuf(basic_streambuf<charT, traits>* sb);

    basic_ios& copyfmt(const basic_ios& rhs);

    char_type fill() const;
    char_type fill(char_type ch);

    locale imbue(const locale& loc);

    char      narrow(char_type c, char dfault) const;
    char_type widen(char c) const;

    basic_ios(const basic_ios&) = delete;
    basic_ios& operator=(const basic_ios&) = delete;

  protected:
    basic_ios();
    void init(basic_streambuf<charT, traits>* sb);
    void move(basic_ios& rhs);
    void move(basic_ios&& rhs);
    void swap(basic_ios& rhs) noexcept;
    void set_rdbuf(basic_streambuf<charT, traits>* sb);

  };
}

29.5.5.2 Constructors [basic.ios.cons]

explicit basic_ios(basic_streambuf<charT, traits>* sb);
Effects: Assigns initial values to its member objects by calling init(sb).
basic_ios();
Effects: Leaves its member objects uninitialized.
The object shall be initialized by calling basic_­ios​::​init before its first use or before it is destroyed, whichever comes first; otherwise the behavior is undefined.
~basic_ios();
Remarks: The destructor does not destroy rdbuf().
void init(basic_streambuf<charT, traits>* sb);
Postconditions: The postconditions of this function are indicated in Table 122.
Table 122: basic_­ios​::​init() effects   [tab:basic.ios.cons]
Element
Value
rdbuf()
sb
tie()
0
rdstate()
goodbit if sb is not a null pointer, otherwise badbit.
exceptions()
goodbit
flags()
skipws | dec
width()
0
precision()
6
fill()
widen(' ')
getloc()
a copy of the value returned by locale()
iarray
a null pointer
parray
a null pointer

29.5.5.3 Member functions [basic.ios.members]

basic_ostream<charT, traits>* tie() const;
Returns: An output sequence that is tied to (synchronized with) the sequence controlled by the stream buffer.
basic_ostream<charT, traits>* tie(basic_ostream<charT, traits>* tiestr);
Preconditions: If tiestr is not null, tiestr is not reachable by traversing the linked list of tied stream objects starting from tiestr->tie().
Postconditions: tiestr == tie().
Returns: The previous value of tie().
basic_streambuf<charT, traits>* rdbuf() const;
Returns: A pointer to the streambuf associated with the stream.
basic_streambuf<charT, traits>* rdbuf(basic_streambuf<charT, traits>* sb);
Effects: Calls clear().
Postconditions: sb == rdbuf().
Returns: The previous value of rdbuf().
locale imbue(const locale& loc);
Effects: Calls ios_­base​::​imbue(loc) and if rdbuf() != 0 then rdbuf()->pubimbue(loc).
Returns: The prior value of ios_­base​::​imbue().
char narrow(char_type c, char dfault) const;
Returns: use_­facet<ctype<char_­type>>(getloc()).narrow(c, dfault)
char_type widen(char c) const;
Returns: use_­facet<ctype<char_­type>>(getloc()).widen(c)
char_type fill() const;
Returns: The character used to pad (fill) an output conversion to the specified field width.
char_type fill(char_type fillch);
Postconditions: traits​::​eq(fillch, fill()).
Returns: The previous value of fill().
basic_ios& copyfmt(const basic_ios& rhs);
Effects: If (this == addressof(rhs)) is true does nothing.
Otherwise assigns to the member objects of *this the corresponding member objects of rhs as follows:
  • calls each registered callback pair (fn, idx) as (*fn)(erase_­event, *this, idx);
  • then, assigns to the member objects of *this the corresponding member objects of rhs, except that
    • rdstate(), rdbuf(), and exceptions() are left unchanged;
    • the contents of arrays pointed at by pword and iword are copied, not the pointers themselves;292 and
    • if any newly stored pointer values in *this point at objects stored outside the object rhs and those objects are destroyed when rhs is destroyed, the newly stored pointer values are altered to point at newly constructed copies of the objects;
  • then, calls each callback pair that was copied from rhs as (*fn)(copyfmt_­event, *this, idx);
  • then, calls exceptions(rhs.exceptions()).
[Note
:
The second pass through the callback pairs permits a copied pword value to be zeroed, or to have its referent deep copied or reference counted, or to have other special action taken.
— end note
]
Postconditions: The postconditions of this function are indicated in Table 123.
Table 123: basic_­ios​::​copyfmt() effects   [tab:basic.ios.copyfmt]
Element
Value
rdbuf()
unchanged
tie()
rhs.tie()
rdstate()
unchanged
exceptions()
rhs.exceptions()
flags()
rhs.flags()
width()
rhs.width()
precision()
rhs.precision()
fill()
rhs.fill()
getloc()
rhs.getloc()
Returns: *this.
void move(basic_ios& rhs); void move(basic_ios&& rhs);
Postconditions: *this has the state that rhs had before the function call, except that rdbuf() returns nullptr.
rhs is in a valid but unspecified state, except that rhs.rdbuf() returns the same value as it returned before the function call, and rhs.tie() returns nullptr.
void swap(basic_ios& rhs) noexcept;
Effects: The states of *this and rhs are exchanged, except that rdbuf() returns the same value as it returned before the function call, and rhs.rdbuf() returns the same value as it returned before the function call.
void set_rdbuf(basic_streambuf<charT, traits>* sb);
Preconditions: sb != nullptr is true.
Effects: Associates the basic_­streambuf object pointed to by sb with this stream without calling clear().
Postconditions: rdbuf() == sb is true.
Throws: Nothing.
This suggests an infinite amount of copying, but the implementation can keep track of the maximum element of the arrays that is nonzero.
⮥

29.5.5.4 Flags functions [iostate.flags]

explicit operator bool() const;
Returns: !fail().
bool operator!() const;
Returns: fail().
iostate rdstate() const;
Returns: The error state of the stream buffer.
void clear(iostate state = goodbit);
Postconditions: If rdbuf() != 0 then state == rdstate(); otherwise rdstate() == (state | ios_­base​::​badbit).
Effects: If ((state | (rdbuf() ? goodbit : badbit)) & exceptions()) == 0, returns.
Otherwise, the function throws an object of class ios_­base​::​failure ([ios.failure]), constructed with implementation-defined argument values.
void setstate(iostate state);
Effects: Calls clear(rdstate() | state) (which may throw ios_­base​::​failure ([ios.failure])).
bool good() const;
Returns: rdstate() == 0
bool eof() const;
Returns: true if eofbit is set in rdstate().
bool fail() const;
Returns: true if failbit or badbit is set in rdstate().293
bool bad() const;
Returns: true if badbit is set in rdstate().
iostate exceptions() const;
Returns: A mask that determines what elements set in rdstate() cause exceptions to be thrown.
void exceptions(iostate except);
Postconditions: except == exceptions().
Effects: Calls clear(rdstate()).
Checking badbit also for fail() is historical practice.
⮥