30 Input/output library [input.output]

30.5 Iostreams base classes [iostreams.base]

30.5.3 Class ios_­base [ios.base]

namespace std {
  class ios_base {
  public:
    class failure; // see below

    // [ios::fmtflags], fmtflags
    using fmtflags = T1;
    static constexpr fmtflags boolalpha = unspecified;
    static constexpr fmtflags dec = unspecified;
    static constexpr fmtflags fixed = unspecified;
    static constexpr fmtflags hex = unspecified;
    static constexpr fmtflags internal = unspecified;
    static constexpr fmtflags left = unspecified;
    static constexpr fmtflags oct = unspecified;
    static constexpr fmtflags right = unspecified;
    static constexpr fmtflags scientific = unspecified;
    static constexpr fmtflags showbase = unspecified;
    static constexpr fmtflags showpoint = unspecified;
    static constexpr fmtflags showpos = unspecified;
    static constexpr fmtflags skipws = unspecified;
    static constexpr fmtflags unitbuf = unspecified;
    static constexpr fmtflags uppercase = unspecified;
    static constexpr fmtflags adjustfield = see below;
    static constexpr fmtflags basefield = see below;
    static constexpr fmtflags floatfield = see below;

    // [ios::iostate], iostate
    using iostate = T2;
    static constexpr iostate badbit = unspecified;
    static constexpr iostate eofbit = unspecified;
    static constexpr iostate failbit = unspecified;
    static constexpr iostate goodbit = see below;

    // [ios::openmode], openmode
    using openmode = T3;
    static constexpr openmode app = unspecified;
    static constexpr openmode ate = unspecified;
    static constexpr openmode binary = unspecified;
    static constexpr openmode in = unspecified;
    static constexpr openmode out = unspecified;
    static constexpr openmode trunc = unspecified;

    // [ios::seekdir], seekdir
    using seekdir = T4;
    static constexpr seekdir beg = unspecified;
    static constexpr seekdir cur = unspecified;
    static constexpr seekdir end = unspecified;

    class Init;

    // [fmtflags.state], fmtflags state
    fmtflags flags() const;
    fmtflags flags(fmtflags fmtfl);
    fmtflags setf(fmtflags fmtfl);
    fmtflags setf(fmtflags fmtfl, fmtflags mask);
    void unsetf(fmtflags mask);

    streamsize precision() const;
    streamsize precision(streamsize prec);
    streamsize width() const;
    streamsize width(streamsize wide);

    // [ios.base.locales], locales
    locale imbue(const locale& loc);
    locale getloc() const;

    // [ios.base.storage], storage
    static int xalloc();
    long&  iword(int index);
    void*& pword(int index);

    // destructor:
    virtual ~ios_base();

    // [ios.base.callback], callbacks;
    enum event { erase_event, imbue_event, copyfmt_event };
    using event_callback = void (*)(event, ios_base&, int index);
    void register_callback(event_callback fn, int index);

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

    static bool sync_with_stdio(bool sync = true);

  protected:
    ios_base();

  private:
    static int index;  // exposition only
    long*  iarray;     // exposition only
    void** parray;     // exposition only
  };
}

ios_­base defines several member types:

It maintains several kinds of data:

[Note: For the sake of exposition, the maintained data is presented here as:

end note]

30.5.3.1 Types [ios.types]

30.5.3.1.1 Class ios_­base​::​failure [ios::failure]

namespace std {
  class ios_base::failure : public system_error {
  public:
    explicit failure(const string& msg, const error_code& ec = io_errc::stream);
    explicit failure(const char* msg, const error_code& ec = io_errc::stream);
  };
}

An implementation is permitted to define ios_­base​::​failure as a synonym for a class with equivalent functionality to class ios_­base​::​failure shown in this subclause. [Note: When ios_­base​::​failure is a synonym for another type it shall provide a nested type failure, to emulate the injected class name. end note] The class failure defines the base class for the types of all objects thrown as exceptions, by functions in the iostreams library, to report errors detected during stream buffer operations.

When throwing ios_­base​::​failure exceptions, implementations should provide values of ec that identify the specific reason for the failure. [Note: Errors arising from the operating system would typically be reported as system_­category() errors with an error value of the error number reported by the operating system. Errors arising from within the stream library would typically be reported as error_­code(io_­errc​::​stream, iostream_­category()). end note]

explicit failure(const string& msg, const error_code& ec = io_errc::stream);

Effects: Constructs an object of class failure by constructing the base class with msg and ec.

explicit failure(const char* msg, const error_code& ec = io_errc::stream);

Effects: Constructs an object of class failure by constructing the base class with msg and ec.

30.5.3.1.2 Type ios_­base​::​fmtflags [ios::fmtflags]

using fmtflags = T1;

The type fmtflags is a bitmask type. Setting its elements has the effects indicated in Table 107.

Table 107fmtflags effects
ElementEffect(s) if set
boolalpha insert and extract bool type in alphabetic format
dec converts integer input or generates integer output in decimal base
fixed generate floating-point output in fixed-point notation
hex converts integer input or generates integer output in hexadecimal base
internal adds fill characters at a designated internal point in certain generated output, or identical to right if no such point is designated
left adds fill characters on the right (final positions) of certain generated output
oct converts integer input or generates integer output in octal base
right adds fill characters on the left (initial positions) of certain generated output
scientific generates floating-point output in scientific notation
showbase generates a prefix indicating the numeric base of generated integer output
showpoint generates a decimal-point character unconditionally in generated floating-point output
showpos generates a + sign in non-negative generated numeric output
skipws skips leading whitespace before certain input operations
unitbuf flushes output after each output operation
uppercase replaces certain lowercase letters with their uppercase equivalents in generated output

Type fmtflags also defines the constants indicated in Table 108.

Table 108fmtflags constants
ConstantAllowable values
adjustfield left | right | internal
basefield dec | oct | hex
floatfield scientific | fixed

30.5.3.1.3 Type ios_­base​::​iostate [ios::iostate]

using iostate = T2;

The type iostate is a bitmask type that contains the elements indicated in Table 109.

Table 109iostate effects
ElementEffect(s) if set
badbit indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error from a file);
eofbit indicates that an input operation reached the end of an input sequence;
failbit indicates that an input operation failed to read the expected characters, or that an output operation failed to generate the desired characters.

Type iostate also defines the constant:

  • goodbit, the value zero.

30.5.3.1.4 Type ios_­base​::​openmode [ios::openmode]

using openmode = T3;

The type openmode is a bitmask type. It contains the elements indicated in Table 110.

Table 110openmode effects
ElementEffect(s) if set
app seek to end before each write
ate open and seek to end immediately after opening
binary perform input and output in binary mode (as opposed to text mode)
in open for input
out open for output
trunc truncate an existing stream when opening

30.5.3.1.5 Type ios_­base​::​seekdir [ios::seekdir]

using seekdir = T4;

The type seekdir is an enumerated type that contains the elements indicated in Table 111.

Table 111seekdir effects
ElementMeaning
beg request a seek (for subsequent input or output) relative to the beginning of the stream
cur request a seek relative to the current position within the sequence
end request a seek relative to the current end of the sequence

30.5.3.1.6 Class ios_­base​::​Init [ios::Init]

namespace std {
  class ios_base::Init {
  public:
    Init();
    ~Init();
  private:
    static int init_cnt; // exposition only
  };
}

The class Init describes an object whose construction ensures the construction of the eight objects declared in <iostream> ([iostream.objects]) that associate file stream buffers with the standard C streams provided for by the functions declared in <cstdio>.

For the sake of exposition, the maintained data is presented here as:

  • static int init_­cnt, counts the number of constructor and destructor calls for class Init, initialized to zero.

Init();

Effects: Constructs an object of class Init. Constructs and initializes the objects cin, cout, cerr, clog, wcin, wcout, wcerr, and wclog if they have not already been constructed and initialized.

~Init();

Effects: Destroys an object of class Init. If there are no other instances of the class still in existence, calls cout.flush(), cerr.flush(), clog.flush(), wcout.flush(), wcerr.flush(), wclog.flush().

30.5.3.2 ios_­base state functions [fmtflags.state]

fmtflags flags() const;

Returns: The format control information for both input and output.

fmtflags flags(fmtflags fmtfl);

Postconditions: fmtfl == flags().

Returns: The previous value of flags().

fmtflags setf(fmtflags fmtfl);

Effects: Sets fmtfl in flags().

Returns: The previous value of flags().

fmtflags setf(fmtflags fmtfl, fmtflags mask);

Effects: Clears mask in flags(), sets fmtfl & mask in flags().

Returns: The previous value of flags().

void unsetf(fmtflags mask);

Effects: Clears mask in flags().

streamsize precision() const;

Returns: The precision to generate on certain output conversions.

streamsize precision(streamsize prec);

Postconditions: prec == precision().

Returns: The previous value of precision().

streamsize width() const;

Returns: The minimum field width (number of characters) to generate on certain output conversions.

streamsize width(streamsize wide);

Postconditions: wide == width().

Returns: The previous value of width().

30.5.3.3 ios_­base functions [ios.base.locales]

locale imbue(const locale& loc);

Effects: Calls each registered callback pair (fn, index) ([ios.base.callback]) as (*fn)(imbue_­event, *this, index) at such a time that a call to ios_­base​::​getloc() from within fn returns the new locale value loc.

Returns: The previous value of getloc().

Postconditions: loc == getloc().

locale getloc() const;

Returns: If no locale has been imbued, a copy of the global C++ locale, locale(), in effect at the time of construction. Otherwise, returns the imbued locale, to be used to perform locale-dependent input and output operations.

30.5.3.4 ios_­base static members [ios.members.static]

bool sync_with_stdio(bool sync = true);

Returns: true if the previous state of the standard iostream objects was synchronized and otherwise returns false. The first time it is called, the function returns true.

Effects: If any input or output operation has occurred using the standard streams prior to the call, the effect is implementation-defined. Otherwise, called with a false argument, it allows the standard streams to operate independently of the standard C streams.

When a standard iostream object str is synchronized with a standard stdio stream f, the effect of inserting a character c by

fputc(f, c);

is the same as the effect of

str.rdbuf()->sputc(c);

for any sequences of characters; the effect of extracting a character c by

c = fgetc(f);

is the same as the effect of

c = str.rdbuf()->sbumpc();

for any sequences of characters; and the effect of pushing back a character c by

ungetc(c, f);

is the same as the effect of

str.rdbuf()->sputbackc(c);

for any sequence of characters.292

This implies that operations on a standard iostream object can be mixed arbitrarily with operations on the corresponding stdio stream. In practical terms, synchronization usually means that a standard iostream object and a standard stdio object share a buffer.

30.5.3.5 ios_­base storage functions [ios.base.storage]

static int xalloc();

Returns: index ++.

Remarks: Concurrent access to this function by multiple threads shall not result in a data race.

long& iword(int idx);

Effects: If iarray is a null pointer, allocates an array of long of unspecified size and stores a pointer to its first element in iarray. The function then extends the array pointed at by iarray as necessary to include the element iarray[idx]. Each newly allocated element of the array is initialized to zero. The reference returned is invalid after any other operations on the object.293 However, the value of the storage referred to is retained, so that until the next call to copyfmt, calling iword with the same index yields another reference to the same value. If the function fails294 and *this is a base class subobject of a basic_­ios<> object or subobject, the effect is equivalent to calling basic_­ios<>​::​setstate(badbit) on the derived object (which may throw failure).

Returns: On success iarray[idx]. On failure, a valid long& initialized to 0.

void*& pword(int idx);

Effects: If parray is a null pointer, allocates an array of pointers to void of unspecified size and stores a pointer to its first element in parray. The function then extends the array pointed at by parray as necessary to include the element parray[idx]. Each newly allocated element of the array is initialized to a null pointer. The reference returned is invalid after any other operations on the object. However, the value of the storage referred to is retained, so that until the next call to copyfmt, calling pword with the same index yields another reference to the same value. If the function fails295 and *this is a base class subobject of a basic_­ios<> object or subobject, the effect is equivalent to calling basic_­ios<>​::​setstate(badbit) on the derived object (which may throw failure).

Returns: On success parray[idx]. On failure a valid void*& initialized to 0.

Remarks: After a subsequent call to pword(int) for the same object, the earlier return value may no longer be valid.

An implementation is free to implement both the integer array pointed at by iarray and the pointer array pointed at by parray as sparse data structures, possibly with a one-element cache for each.

for example, because it cannot allocate space.

for example, because it cannot allocate space.

30.5.3.6 ios_­base callbacks [ios.base.callback]

void register_callback(event_callback fn, int index);

Effects: Registers the pair (fn, index) such that during calls to imbue() ([ios.base.locales]), copyfmt(), or ~ios_­base() ([ios.base.cons]), the function fn is called with argument index. Functions registered are called when an event occurs, in opposite order of registration. Functions registered while a callback function is active are not called until the next event.

Requires: The function fn shall not throw exceptions.

Remarks: Identical pairs are not merged. A function registered twice will be called twice.

30.5.3.7 ios_­base constructors/destructor [ios.base.cons]

ios_base();

Effects: Each ios_­base member has an indeterminate value after construction. The object's members shall be initialized by calling basic_­ios​::​init before the object's first use or before it is destroyed, whichever comes first; otherwise the behavior is undefined.

~ios_base();

Effects: Destroys an object of class ios_­base. Calls each registered callback pair (fn, index) ([ios.base.callback]) as (*fn)(​erase_­event, *this, index) at such time that any ios_­base member function called from within fn has well defined results.