[ios.base] (original) (raw)

31 Input/output library [input.output]

31.5 Iostreams base classes [iostreams.base]

31.5.2 Class ios_base [ios.base]

31.5.2.1 General [ios.base.general]

namespace std { class ios_base { public: class failure; 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;using iostate = T2;static constexpr iostate badbit = unspecified;static constexpr iostate eofbit = unspecified;static constexpr iostate failbit = unspecified;static constexpr iostate goodbit = see below;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 noreplace = unspecified;static constexpr openmode out = unspecified;static constexpr openmode trunc = unspecified;using seekdir = T4;static constexpr seekdir beg = unspecified;static constexpr seekdir cur = unspecified;static constexpr seekdir end = unspecified;class Init; 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); locale imbue(const locale& loc); locale getloc() const;static int xalloc();long& iword(int idx);void*& pword(int idx);virtual ~ios_base();enum event { erase_event, imbue_event, copyfmt_event };using event_callback = void (*)(event, ios_base&, int idx);void register_callback(event_callback fn, int idx); 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; long* iarray; void** parray; };}

ios_basedefines several member types:

It maintains several kinds of data:

[Note 1:

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

— _end note_]

31.5.2.2 Types [ios.types]

31.5.2.2.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​::​failureas a synonym for a class with equivalent functionality to class ios_base​::​failure shown in this subclause.

[Note 1:

When ios_base​::​failure is a synonym for another type, that type needs to provide a nested type failureto emulate the injected-class-name.

— _end note_]

The classfailuredefines 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 2:

Errors arising from the operating system would typically be reported assystem_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 the base class with msg and ec.

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

Effects: Constructs the base class with msg and ec.

31.5.2.2.2 Type ios_base​::​fmtflags [ios.fmtflags]

Setting its elements has the effects indicated in Table 134.

Table 134 — fmtflags effects [tab:ios.fmtflags]

🔗Element Effect(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

Typefmtflagsalso defines the constants indicated in Table 135.

31.5.2.2.3 Type ios_base​::​iostate [ios.iostate]

The typeiostateis a bitmask type ([bitmask.types]) that contains the elements indicated in Table 136.

Table 136 — iostate effects [tab:ios.iostate]

indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error from a file);
indicates that an input operation reached the end of an input sequence;
indicates that an input operation failed to read the expected characters, or that an output operation failed to generate the desired characters.

Typeiostatealso defines the constant:

31.5.2.2.4 Type ios_base​::​openmode [ios.openmode]

It contains the elements indicated in Table 137.

Table 137 — openmode effects [tab:ios.openmode]

🔗Element Effect(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
🔗noreplace open in exclusive mode
🔗out open for output
🔗trunc truncate an existing stream when opening

31.5.2.2.5 Type ios_base​::​seekdir [ios.seekdir]

The typeseekdiris an enumerated type ([enumerated.types]) that contains the elements indicated in Table 138.

Table 138 — seekdir effects [tab:ios.seekdir]

request a seek (for subsequent input or output) relative to the beginning of the stream
request a seek relative to the current position within the sequence
request a seek relative to the current end of the sequence

31.5.2.2.6 Class ios_base​::​Init [ios.init]

namespace std { class ios_base::Init { public: Init(); Init(const Init&) = default;~Init(); Init& operator=(const Init&) = default;};}

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

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

Effects: If there are no other instances of the class still in existence, callscout.flush(),cerr.flush(),clog.flush(),wcout.flush(),wcerr.flush(),wclog.flush().

31.5.2.3 State functions [fmtflags.state]

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

fmtflags flags(fmtflags fmtfl);

Postconditions: fmtfl == flags().

Returns: The previous value offlags().

fmtflags setf(fmtflags fmtfl);

Effects: Sets fmtfl inflags().

Returns: The previous value offlags().

fmtflags setf(fmtflags fmtfl, fmtflags mask);

Effects: Clears mask inflags(), setsfmtfl & maskinflags().

Returns: The previous value offlags().

void unsetf(fmtflags mask);

Effects: Clears mask inflags().

streamsize precision() const;

Returns: The precision to generate on certain output conversions.

streamsize precision(streamsize prec);

Postconditions: prec == precision().

Returns: The previous value ofprecision().

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

31.5.2.4 Functions [ios.base.locales]

locale imbue(const locale& loc);

Effects: Calls each registered callback pair(fn, idx) ([ios.base.callback]) as(*fn)(imbue_event, *this, idx)at such a time that a call toios_base​::​getloc()from withinfnreturns the new locale valueloc.

Postconditions: loc == getloc().

Returns: The previous value ofgetloc().

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.

31.5.2.5 Static members [ios.members.static]

static bool sync_with_stdio(bool sync = true);

Effects: If any input or output operation has occurred using the standard streams prior to the call, the effect isimplementation-defined.

Otherwise, called with a false argument, it allows the standard streams to operate independently of the standard C streams.

Returns: trueif the previous state of the standard iostream objectswas synchronized and otherwise returnsfalse.

The first time it is called, the function returnstrue.

Remarks: When a standard iostream object str is_synchronized_with a standard stdio stream f, the effect of inserting a character c byfputc(f, c);is the same as the effect ofstr.rdbuf()->sputc(c);for any sequences of characters; the effect of extracting a character c byc = fgetc(f);is the same as the effect ofc = str.rdbuf()->sbumpc();for any sequences of characters; and the effect of pushing back a character c byungetc(c, f);is the same as the effect ofstr.rdbuf()->sputbackc(c);for any sequence of characters.260

31.5.2.6 Storage functions [ios.base.storage]

Remarks: Concurrent access to this function by multiple threads does not result in adata race.

Preconditions: idx is a value obtained by a call to xalloc.

Effects: If iarray is a null pointer, allocates an array oflongof 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 operation on the object.261

However, the value of the storage referred to is retained, so that until the next call tocopyfmt, callingiwordwith the same index yields another reference to the same value.

If the function fails262and*thisis a base class subobject of abasic_ios<>object or subobject, the effect is equivalent to callingbasic_ios<>​::​setstate(badbit)on the derived object (which may throwfailure).

Returns: On success_iarray_[idx].

On failure, a validlong&initialized to 0.

Preconditions: idx is a value obtained by a call to xalloc.

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 operation on the object.

However, the value of the storage referred to is retained, so that until the next call tocopyfmt, callingpwordwith the same index yields another reference to the same value.

If the function fails263and*thisis a base class subobject of abasic_ios<>object or subobject, the effect is equivalent to callingbasic_ios<>​::​setstate(badbit)on the derived object (which may throwfailure).

Returns: On successparray[idx].

On failure a validvoid*&initialized to 0.

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

31.5.2.7 Callbacks [ios.base.callback]

void register_callback(event_callback fn, int idx);

Preconditions: The functionfndoes not throw exceptions.

Effects: Registers the pair(fn, idx)such that during calls toimbue() ([ios.base.locales]),copyfmt(), or~ios_base() ([ios.base.cons]), the functionfnis called with argumentidx.

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.

Remarks: Identical pairs are not merged.

A function registered twice will be called twice.

31.5.2.8 Constructors and destructor [ios.base.cons]

Effects: Eachios_basemember has an indeterminate value after construction.

The object's members shall be initialized by callingbasic_ios​::​initbefore the object's first use or before it is destroyed, whichever comes first; otherwise the behavior is undefined.

Effects: Calls each registered callback pair(fn, idx) ([ios.base.callback]) as(*fn)(​erase_event, *this, idx)at such time that anyios_basemember function called from withinfnhas well-defined results.

Then, any memory obtained is deallocated.