Working Draft, Standard for Programming Language C++ (N4713, 2017 year) - page 36

 

  Главная      Manuals     Working Draft, Standard for Programming Language C++ (N4713, 2017 year)

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     34      35      36      37     ..

 

 

 

Working Draft, Standard for Programming Language C++ (N4713, 2017 year) - page 36

 

 

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
};
}
1
The class Init describes an object whose construction ensures the construction of the eight objects declared
in <iostream> (30.4) that associate file stream buffers with the standard C streams provided for by the
functions declared in <cstdio> (30.12.1).
2
For the sake of exposition, the maintained data is presented here as:
(2.1)
static int init_cnt, counts the number of constructor and destructor calls for class Init, initialized
to zero.
Init();
3
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();
4
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;
1
Returns: The format control information for both input and output.
fmtflags flags(fmtflags fmtfl);
2
Postconditions: fmtfl == flags().
3
Returns: The previous value of flags().
fmtflags setf(fmtflags fmtfl);
4
Effects: Sets fmtfl in flags().
5
Returns: The previous value of flags().
fmtflags setf(fmtflags fmtfl, fmtflags mask);
6
Effects: Clears mask in flags(), sets fmtfl & mask in flags().
7
Returns: The previous value of flags().
void unsetf(fmtflags mask);
8
Effects: Clears mask in flags().
streamsize precision() const;
9
Returns: The precision to generate on certain output conversions.
streamsize precision(streamsize prec);
10
Postconditions: prec == precision().
11
Returns: The previous value of precision().
streamsize width() const;
12
Returns: The minimum field width (number of characters) to generate on certain output conversions.
streamsize width(streamsize wide);
13
Postconditions: wide == width().
§ 30.5.3.2
1042
14
Returns: The previous value of width().
30.5.3.3
ios_base functions
[ios.base.locales]
locale imbue(const locale& loc);
1
Effects: Calls each registered callback pair (fn, index) (30.5.3.6) 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.
2
Returns: The previous value of getloc().
3
Postconditions: loc == getloc().
locale getloc() const;
4
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);
1
Returns: true if the previous state of the standard iostream objects (30.4) was synchronized and
otherwise returns false. The first time it is called, the function returns true.
2
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.
3
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.295
30.5.3.5
ios_base storage functions
[ios.base.storage]
static int xalloc();
1
Returns: index ++.
2
Remarks: Concurrent access to this function by multiple threads shall not result in a data race (6.8.2).
long& iword(int idx);
3
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.
295) 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
1043
The reference returned is invalid after any other operations on the object.296 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 fails297 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).
4
Returns: On success iarray[idx]. On failure, a valid long& initialized to 0.
void*& pword(int idx);
5
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 fails298 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).
6
Returns: On success parray[idx]. On failure a valid void*& initialized to 0.
7
Remarks: After a subsequent call to pword(int) for the same object, the earlier return value may no
longer be valid.
30.5.3.6
ios_base callbacks
[ios.base.callback]
void register_callback(event_callback fn, int index);
1
Effects: Registers the pair (fn, index) such that during calls to imbue() (30.5.3.3), copyfmt(), or
~ios_base() (30.5.3.7), 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.
2
Requires: The function fn shall not throw exceptions.
3
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();
1
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();
2
Effects: Destroys an object of class ios_base. Calls each registered callback pair (fn, index) (30.5.3.6)
as (*fn)(erase_event, *this, index) at such time that any ios_base member function called from
within fn has well-defined results.
30.5.4
Class template fpos
[fpos]
namespace std {
template<class stateT> class fpos {
public:
// 30.5.4.1, members
stateT state() const;
void state(stateT);
private;
stateT st; // exposition only
};
}
296) 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.
297) For example, because it cannot allocate space.
298) For example, because it cannot allocate space.
§ 30.5.4
1044
30.5.4.1
fpos members
[fpos.members]
void state(stateT s);
1
Effects: Assigns s to st.
stateT state() const;
2
Returns: Current value of st.
30.5.4.2
fpos requirements
[fpos.operations]
1
Operations specified in Table 112 are permitted. In that table,
(1.1)
P refers to an instance of fpos,
(1.2)
p and q refer to values of type P,
(1.3)
O refers to type streamoff,
(1.4)
o refers to a value of type streamoff,
(1.5)
sz refers to a value of type streamsize and
(1.6)
i refers to a value of type int.
Table 112 — Position type requirements
Expression
Return type
Operational
Assertion/note
semantics
pre-/post-condition
P(i)
p == P(i)
note: a destructor is assumed.
P p(i);
Postconditions: p == P(i).
P p = i;
P(o)
fpos
converts from offset
O(p)
streamoff
converts to offset
P(O(p)) == p
p == q
convertible to bool
== is an equivalence relation
p != q
convertible to bool
!(p == q)
q = p + o
fpos
+ offset
q - o == p
p += o
q = p - o
fpos
- offset
q + o == p
p -= o
o = p - q
streamoff
distance
q + o == p
streamsize(o)
streamsize
converts
streamsize(O(sz)) == sz
O(sz)
streamoff
converts
streamsize(O(sz)) == sz
2
[Note: Every implementation is required to supply overloaded operators on fpos objects to satisfy the
requirements of 30.5.4.2. It is unspecified whether these operators are members of fpos, global operators, or
provided in some other way.
— end note ]
3
Stream operations that return a value of type traits::pos_type return P(O(-1)) as an invalid value to
signal an error. If this value is used as an argument to any istream, ostream, or streambuf member that
accepts a value of type traits::pos_type then the behavior of that function is undefined.
30.5.5
Class template basic_ios
[ios]
30.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;
§ 30.5.5.1
1045
// 30.5.5.4, 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);
// 30.5.5.2, constructor/destructor
explicit basic_ios(basic_streambuf<charT, traits>* sb);
virtual ~basic_ios();
// 30.5.5.3, 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);
};
}
30.5.5.2
basic_ios constructors
[basic.ios.cons]
explicit basic_ios(basic_streambuf<charT, traits>* sb);
1
Effects: Constructs an object of class basic_ios, assigning initial values to its member objects by
calling init(sb).
basic_ios();
2
Effects: Constructs an object of class basic_ios (30.5.3.7) leaving 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();
3
Remarks: The destructor does not destroy rdbuf().
§ 30.5.5.2
1046
void init(basic_streambuf<charT, traits>* sb);
4
Postconditions: The postconditions of this function are indicated in Table 113.
Table 113 — basic_ios::init() effects
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
30.5.5.3
Member functions
[basic.ios.members]
basic_ostream<charT, traits>* tie() const;
1
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);
2
Requires: If tiestr is not null, tiestr shall not be reachable by traversing the linked list of tied
stream objects starting from tiestr->tie().
3
Postconditions: tiestr == tie().
4
Returns: The previous value of tie().
basic_streambuf<charT, traits>* rdbuf() const;
5
Returns: A pointer to the streambuf associated with the stream.
basic_streambuf<charT, traits>* rdbuf(basic_streambuf<charT, traits>* sb);
6
Postconditions: sb == rdbuf().
7
Effects: Calls clear().
8
Returns: The previous value of rdbuf().
locale imbue(const locale& loc);
9
Effects: Calls ios_base::imbue(loc) (30.5.3.3) and if rdbuf() != 0 then rdbuf()->pubimbue(loc)
(30.6.3.2.1).
10
Returns: The prior value of ios_base::imbue().
char narrow(char_type c, char dfault) const;
11
Returns: use_facet<ctype<char_type>>(getloc()).narrow(c, dfault)
char_type widen(char c) const;
12
Returns: use_facet<ctype<char_type>>(getloc()).widen(c)
char_type fill() const;
13
Returns: The character used to pad (fill) an output conversion to the specified field width.
char_type fill(char_type fillch);
14
Postconditions: traits::eq(fillch, fill()).
§ 30.5.5.3
1047
15
Returns: The previous value of fill().
basic_ios& copyfmt(const basic_ios& rhs);
16
Effects: If (this == &rhs) does nothing. Otherwise assigns to the member objects of *this the
corresponding member objects of rhs as follows:
(16.1)
calls each registered callback pair (fn, index) as (*fn)(erase_event, *this, index);
(16.2)
then, assigns to the member objects of *this the corresponding member objects of rhs, except
that
(16.2.1)
rdstate(), rdbuf(), and exceptions() are left unchanged;
(16.2.2)
the contents of arrays pointed at by pword and iword are copied, not the pointers themselves;299
and
(16.2.3)
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;
(16.3)
then, calls each callback pair that was copied from rhs as (*fn)(copyfmt_event, *this, index);
(16.4)
then, calls exceptions(rhs.exceptions()).
17
[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 ]
18
Postconditions: The postconditions of this function are indicated in Table 114.
Table 114 — basic_ios::copyfmt() effects
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()
19
Returns: *this.
void
move(basic_ios& rhs);
void
move(basic_ios&& rhs);
20
Postconditions: *this shall have the state that rhs had before the function call, except that rdbuf()
shall return 0. rhs shall be in a valid but unspecified state, except that rhs.rdbuf() shall return the
same value as it returned before the function call, and rhs.tie() shall return 0.
void
swap(basic_ios& rhs) noexcept;
21
Effects: The states of *this and rhs shall be exchanged, except that rdbuf() shall return the same
value as it returned before the function call, and rhs.rdbuf() shall return the same value as it returned
before the function call.
void
set_rdbuf(basic_streambuf<charT, traits>* sb);
22
Requires: sb != nullptr.
23
Effects: Associates the basic_streambuf object pointed to by sb with this stream without calling
clear().
24
Postconditions: rdbuf() == sb.
25
Throws: Nothing.
299) This suggests an infinite amount of copying, but the implementation can keep track of the maximum element of the arrays
that is nonzero.
§ 30.5.5.3
1048
30.5.5.4
basic_ios flags functions
[iostate.flags]
explicit operator bool() const;
1
Returns: !fail().
bool operator!() const;
2
Returns: fail().
iostate rdstate() const;
3
Returns: The error state of the stream buffer.
void clear(iostate state = goodbit);
4
Postconditions: If rdbuf() != 0 then state == rdstate(); otherwise rdstate() == (state |
ios_base::badbit).
5
Effects: If ((state | (rdbuf() ? goodbit : badbit)) & exceptions()) == 0, returns. Oth-
erwise, the function throws an object of class basic_ios::failure (30.5.3.1.1), constructed with
implementation-defined argument values.
void setstate(iostate state);
6
Effects: Calls clear(rdstate() | state) (which may throw basic_ios::failure (30.5.3.1.1)).
bool good() const;
7
Returns: rdstate() == 0
bool eof() const;
8
Returns: true if eofbit is set in rdstate().
bool fail() const;
9
Returns: true if failbit or badbit is set in rdstate().300
bool bad() const;
10
Returns: true if badbit is set in rdstate().
iostate exceptions() const;
11
Returns: A mask that determines what elements set in rdstate() cause exceptions to be thrown.
void exceptions(iostate except);
12
Postconditions: except == exceptions().
13
Effects: Calls clear(rdstate()).
30.5.6
ios_base manipulators
[std.ios.manip]
30.5.6.1
fmtflags manipulators
[fmtflags.manip]
ios_base& boolalpha(ios_base& str);
1
Effects: Calls str.setf(ios_base::boolalpha).
2
Returns: str.
ios_base& noboolalpha(ios_base& str);
3
Effects: Calls str.unsetf(ios_base::boolalpha).
4
Returns: str.
ios_base& showbase(ios_base& str);
5
Effects: Calls str.setf(ios_base::showbase).
6
Returns: str.
300) Checking badbit also for fail() is historical practice.
§ 30.5.6.1
1049
ios_base& noshowbase(ios_base& str);
7
Effects: Calls str.unsetf(ios_base::showbase).
8
Returns: str.
ios_base& showpoint(ios_base& str);
9
Effects: Calls str.setf(ios_base::showpoint).
10
Returns: str.
ios_base& noshowpoint(ios_base& str);
11
Effects: Calls str.unsetf(ios_base::showpoint).
12
Returns: str.
ios_base& showpos(ios_base& str);
13
Effects: Calls str.setf(ios_base::showpos).
14
Returns: str.
ios_base& noshowpos(ios_base& str);
15
Effects: Calls str.unsetf(ios_base::showpos).
16
Returns: str.
ios_base& skipws(ios_base& str);
17
Effects: Calls str.setf(ios_base::skipws).
18
Returns: str.
ios_base& noskipws(ios_base& str);
19
Effects: Calls str.unsetf(ios_base::skipws).
20
Returns: str.
ios_base& uppercase(ios_base& str);
21
Effects: Calls str.setf(ios_base::uppercase).
22
Returns: str.
ios_base& nouppercase(ios_base& str);
23
Effects: Calls str.unsetf(ios_base::uppercase).
24
Returns: str.
ios_base& unitbuf(ios_base& str);
25
Effects: Calls str.setf(ios_base::unitbuf).
26
Returns: str.
ios_base& nounitbuf(ios_base& str);
27
Effects: Calls str.unsetf(ios_base::unitbuf).
28
Returns: str.
30.5.6.2
adjustfield manipulators
[adjustfield.manip]
ios_base& internal(ios_base& str);
1
Effects: Calls str.setf(ios_base::internal, ios_base::adjustfield).
2
Returns: str.
ios_base& left(ios_base& str);
3
Effects: Calls str.setf(ios_base::left, ios_base::adjustfield).
4
Returns: str.
§ 30.5.6.2
1050
ios_base& right(ios_base& str);
5
Effects: Calls str.setf(ios_base::right, ios_base::adjustfield).
6
Returns: str.
30.5.6.3
basefield manipulators
[basefield.manip]
ios_base& dec(ios_base& str);
1
Effects: Calls str.setf(ios_base::dec, ios_base::basefield).
2
Returns: str301.
ios_base& hex(ios_base& str);
3
Effects: Calls str.setf(ios_base::hex, ios_base::basefield).
4
Returns: str.
ios_base& oct(ios_base& str);
5
Effects: Calls str.setf(ios_base::oct, ios_base::basefield).
6
Returns: str.
30.5.6.4
floatfield manipulators
[floatfield.manip]
ios_base& fixed(ios_base& str);
1
Effects: Calls str.setf(ios_base::fixed, ios_base::floatfield).
2
Returns: str.
ios_base& scientific(ios_base& str);
3
Effects: Calls str.setf(ios_base::scientific, ios_base::floatfield).
4
Returns: str.
ios_base& hexfloat(ios_base& str);
5
Effects: Calls str.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield).
6
Returns: str.
7
[ Note: The more obvious use of ios_base::hex to specify hexadecimal floating-point format would change
the meaning of existing well-defined programs. C++ 2003 gives no meaning to the combination of fixed and
scientific. — end note ]
ios_base& defaultfloat(ios_base& str);
8
Effects: Calls str.unsetf(ios_base::floatfield).
9
Returns: str.
30.5.6.5
Error reporting
[error.reporting]
error_code make_error_code(io_errc e) noexcept;
1
Returns: error_code(static_cast<int>(e), iostream_category()).
error_condition make_error_condition(io_errc e) noexcept;
2
Returns: error_condition(static_cast<int>(e), iostream_category()).
const error_category& iostream_category() noexcept;
3
Returns: A reference to an object of a type derived from class error_category.
4
The object’s default_error_condition and equivalent virtual functions shall behave as specified
for the class error_category. The object’s name virtual function shall return a pointer to the string
"iostream".
301) The function signature dec(ios_base&) can be called by the function signature basic_ostream& stream::operator<<(ios_-
base& (*)(ios_base&)) to permit expressions of the form cout << dec to change the format flags stored in cout.
§ 30.5.6.5
1051
30.6
Stream buffers
[stream.buffers]
30.6.1
Header <streambuf> synopsis
[streambuf.syn]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_streambuf;
using streambuf
= basic_streambuf<char>;
using wstreambuf = basic_streambuf<wchar_t>;
}
1
The header <streambuf> defines types that control input from and output to character sequences.
30.6.2
Stream buffer requirements
[streambuf.reqts]
1
Stream buffers can impose various constraints on the sequences they control. Some constraints are:
(1.1)
The controlled input sequence can be not readable.
(1.2)
The controlled output sequence can be not writable.
(1.3)
The controlled sequences can be associated with the contents of other representations for character
sequences, such as external files.
(1.4)
The controlled sequences can support operations directly to or from associated sequences.
(1.5)
The controlled sequences can impose limitations on how the program can read characters from a
sequence, write characters to a sequence, put characters back into an input sequence, or alter the stream
position.
2
Each sequence is characterized by three pointers which, if non-null, all point into the same charT array object.
The array object represents, at any moment, a (sub)sequence of characters from the sequence. Operations
performed on a sequence alter the values stored in these pointers, perform reads and writes directly to or
from associated sequences, and alter “the stream position” and conversion state as needed to maintain this
subsequence relationship. The three pointers are:
(2.1)
the beginning pointer, or lowest element address in the array (called xbeg here);
(2.2)
the next pointer, or next element address that is a current candidate for reading or writing (called
xnext here);
(2.3)
the end pointer, or first element address beyond the end of the array (called xend here).
3
The following semantic constraints shall always apply for any set of three pointers for a sequence, using the
pointer names given immediately above:
(3.1)
If xnext is not a null pointer, then xbeg and xend shall also be non-null pointers into the same charT
array, as described above; otherwise, xbeg and xend shall also be null.
(3.2)
If xnext is not a null pointer and xnext < xend for an output sequence, then a write position is
available. In this case, *xnext shall be assignable as the next element to write (to put, or to store a
character value, into the sequence).
(3.3)
If xnext is not a null pointer and xbeg < xnext for an input sequence, then a putback position is
available. In this case, xnext[-1] shall have a defined value and is the next (preceding) element to
store a character that is put back into the input sequence.
(3.4)
If xnext is not a null pointer and xnext < xend for an input sequence, then a read position is available.
In this case, *xnext shall have a defined value and is the next element to read (to get, or to obtain a
character value, from the sequence).
30.6.3
Class template basic_streambuf
[streambuf]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_streambuf {
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;
§ 30.6.3
1052
virtual ~basic_streambuf();
// 30.6.3.2.1, locales
locale pubimbue(const locale& loc);
locale getloc() const;
// 30.6.3.2.2, buffer and positioning
basic_streambuf* pubsetbuf(char_type* s, streamsize n);
pos_type pubseekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
pos_type pubseekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out);
int
pubsync();
// get and put areas
// 30.6.3.2.3, get area
streamsize in_avail();
int_type snextc();
int_type sbumpc();
int_type sgetc();
streamsize sgetn(char_type* s, streamsize n);
// 30.6.3.2.4, putback
int_type sputbackc(char_type c);
int_type sungetc();
// 30.6.3.2.5, put area
int_type sputc(char_type c);
streamsize sputn(const char_type* s, streamsize n);
protected:
basic_streambuf();
basic_streambuf(const basic_streambuf& rhs);
basic_streambuf& operator=(const basic_streambuf& rhs);
void swap(basic_streambuf& rhs);
// 30.6.3.3.2, get area access
char_type* eback() const;
char_type* gptr() const;
char_type* egptr() const;
void
gbump(int n);
void
setg(char_type* gbeg, char_type* gnext, char_type*
gend);
// 30.6.3.3.3, put area access
char_type* pbase() const;
char_type* pptr() const;
char_type* epptr() const;
void
pbump(int n);
void
setp(char_type* pbeg, char_type* pend);
// 30.6.3.4, virtual functions
// 30.6.3.4.1, locales
virtual void imbue(const locale& loc);
// 30.6.3.4.2, buffer management and positioning
virtual basic_streambuf* setbuf(char_type* s, streamsize n);
virtual pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
§ 30.6.3
1053
virtual pos_type seekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out);
virtual int
sync();
// 30.6.3.4.3, get area
virtual streamsize showmanyc();
virtual streamsize xsgetn(char_type* s, streamsize n);
virtual int_type underflow();
virtual int_type uflow();
// 30.6.3.4.4, putback
virtual int_type pbackfail(int_type c = traits::eof());
// 30.6.3.4.5, put area
virtual streamsize xsputn(const char_type* s, streamsize n);
virtual int_type overflow(int_type c = traits::eof());
};
}
1
The class template basic_streambuf serves as an abstract base class for deriving various stream buffers
whose objects each control two character sequences:
(1.1)
a character input sequence;
(1.2)
a character output sequence.
30.6.3.1
basic_streambuf constructors
[streambuf.cons]
basic_streambuf();
1
Effects: Constructs an object of class basic_streambuf<charT, traits> and initializes:302
(1.1)
all its pointer member objects to null pointers,
(1.2)
the getloc() member to a copy the global locale, locale(), at the time of construction.
2
Remarks: Once the getloc() member is initialized, results of calling locale member functions, and of
members of facets so obtained, can safely be cached until the next time the member imbue is called.
basic_streambuf(const basic_streambuf& rhs);
3
Effects: Constructs a copy of rhs.
4
Postconditions:
(4.1)
eback() == rhs.eback()
(4.2)
gptr() == rhs.gptr()
(4.3)
egptr() == rhs.egptr()
(4.4)
pbase() == rhs.pbase()
(4.5)
pptr() == rhs.pptr()
(4.6)
epptr() == rhs.epptr()
(4.7)
getloc() == rhs.getloc()
~basic_streambuf();
5
Effects: None.
30.6.3.2
basic_streambuf public member functions
[streambuf.members]
30.6.3.2.1
Locales
[streambuf.locales]
locale pubimbue(const locale& loc);
1
Postconditions: loc == getloc().
2
Effects: Calls imbue(loc).
302) The default constructor is protected for class basic_streambuf to assure that only objects for classes derived from this
class may be constructed.
§ 30.6.3.2.1
1054
3
Returns: Previous value of getloc().
locale getloc() const;
4
Returns: If pubimbue() has ever been called, then the last value of loc supplied, otherwise the current
global locale, locale(), in effect at the time of construction. If called after pubimbue() has been called
but before pubimbue has returned (i.e., from within the call of imbue()) then it returns the previous
value.
30.6.3.2.2
Buffer management and positioning
[streambuf.buffer]
basic_streambuf* pubsetbuf(char_type* s, streamsize n);
1
Returns: setbuf(s, n).
pos_type pubseekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
2
Returns: seekoff(off, way, which).
pos_type pubseekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out);
3
Returns: seekpos(sp, which).
int pubsync();
4
Returns: sync().
30.6.3.2.3
Get area
[streambuf.pub.get]
streamsize in_avail();
1
Returns: If a read position is available, returns egptr() - gptr(). Otherwise returns showmanyc()
(30.6.3.4.3).
int_type snextc();
2
Effects: Calls sbumpc().
3
Returns: If that function returns traits::eof(), returns traits::eof(). Otherwise, returns sgetc().
int_type sbumpc();
4
Returns: If the input sequence read position is not available, returns uflow(). Otherwise, returns
traits::to_int_type(*gptr()) and increments the next pointer for the input sequence.
int_type sgetc();
5
Returns: If the input sequence read position is not available, returns underflow(). Otherwise, returns
traits::to_int_type(*gptr()).
streamsize sgetn(char_type* s, streamsize n);
6
Returns: xsgetn(s, n).
30.6.3.2.4
Putback
[streambuf.pub.pback]
int_type sputbackc(char_type c);
1
Returns: If the input sequence putback position is not available, or if traits::eq(c, gptr()[-1])
is false, returns pbackfail(traits::to_int_type(c)). Otherwise, decrements the next pointer for
the input sequence and returns traits::to_int_type(*gptr()).
int_type sungetc();
2
Returns: If the input sequence putback position is not available, returns pbackfail(). Otherwise,
decrements the next pointer for the input sequence and returns traits::to_int_type(*gptr()).
§ 30.6.3.2.4
1055
30.6.3.2.5
Put area
[streambuf.pub.put]
int_type sputc(char_type c);
1
Returns: If the output sequence write position is not available, returns overflow(traits::to_int_-
type(c)). Otherwise, stores c at the next pointer for the output sequence, increments the pointer, and
returns traits::to_int_type(c).
streamsize sputn(const char_type* s, streamsize n);
2
Returns: xsputn(s, n).
30.6.3.3
basic_streambuf protected member functions
[streambuf.protected]
30.6.3.3.1
Assignment
[streambuf.assign]
basic_streambuf& operator=(const basic_streambuf& rhs);
1
Effects: Assigns the data members of rhs to *this.
2
Postconditions:
(2.1)
eback() == rhs.eback()
(2.2)
gptr() == rhs.gptr()
(2.3)
egptr() == rhs.egptr()
(2.4)
pbase() == rhs.pbase()
(2.5)
pptr() == rhs.pptr()
(2.6)
epptr() == rhs.epptr()
(2.7)
getloc() == rhs.getloc()
3
Returns: *this.
void swap(basic_streambuf& rhs);
4
Effects: Swaps the data members of rhs and *this.
30.6.3.3.2
Get area access
[streambuf.get.area]
char_type* eback() const;
1
Returns: The beginning pointer for the input sequence.
char_type* gptr() const;
2
Returns: The next pointer for the input sequence.
char_type* egptr() const;
3
Returns: The end pointer for the input sequence.
void gbump(int n);
4
Effects: Adds n to the next pointer for the input sequence.
void setg(char_type* gbeg, char_type* gnext, char_type* gend);
5
Postconditions: gbeg == eback(), gnext == gptr(), and gend
==
egptr().
30.6.3.3.3
Put area access
[streambuf.put.area]
char_type* pbase() const;
1
Returns: The beginning pointer for the output sequence.
char_type* pptr() const;
2
Returns: The next pointer for the output sequence.
char_type* epptr() const;
3
Returns: The end pointer for the output sequence.
§ 30.6.3.3.3
1056
void pbump(int n);
4
Effects: Adds n to the next pointer for the output sequence.
void setp(char_type* pbeg, char_type* pend);
5
Postconditions: pbeg == pbase(), pbeg == pptr(), and pend == epptr().
30.6.3.4
basic_streambuf virtual functions
[streambuf.virtuals]
30.6.3.4.1
Locales
[streambuf.virt.locales]
void imbue(const locale&);
1
Effects: Change any translations based on locale.
2
Remarks: Allows the derived class to be informed of changes in locale at the time they occur. Between
invocations of this function a class derived from streambuf can safely cache results of calls to locale
functions and to members of facets so obtained.
3
Default behavior: Does nothing.
30.6.3.4.2
Buffer management and positioning
[streambuf.virt.buffer]
basic_streambuf* setbuf(char_type* s, streamsize n);
1
Effects: Influences stream buffering in a way that is defined separately for each class derived from
basic_streambuf in this Clause (30.8.2.4, 30.9.2.4).
2
Default behavior: Does nothing. Returns this.
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
3
Effects: Alters the stream positions within one or more of the controlled sequences in a way that is
defined separately for each class derived from basic_streambuf in this Clause (30.8.2.4, 30.9.2.4).
4
Default behavior: Returns pos_type(off_type(-1)).
pos_type seekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out);
5
Effects: Alters the stream positions within one or more of the controlled sequences in a way that is
defined separately for each class derived from basic_streambuf in this Clause (30.8.2, 30.9.2).
6
Default behavior: Returns pos_type(off_type(-1)).
int sync();
7
Effects: Synchronizes the controlled sequences with the arrays. That is, if pbase() is non-null the
characters between pbase() and pptr() are written to the controlled sequence. The pointers may then
be reset as appropriate.
8
Returns: -1 on failure. What constitutes failure is determined by each derived class (30.9.2.4).
9
Default behavior: Returns zero.
30.6.3.4.3
Get area
[streambuf.virt.get]
streamsize showmanyc();303
1
Returns: An estimate of the number of characters available in the sequence, or -1. If it returns a
positive value, then successive calls to underflow() will not return traits::eof() until at least that
number of characters have been extracted from the stream. If showmanyc() returns -1, then calls to
underflow() or uflow() will fail.304
2
Default behavior: Returns zero.
3
Remarks: Uses traits::eof().
303) The morphemes of showmanyc are “es-how-many-see”, not “show-manic”.
304) underflow or uflow might fail by throwing an exception prematurely. The intention is not only that the calls will not
return eof() but that they will return “immediately”.
§ 30.6.3.4.3
1057
streamsize xsgetn(char_type* s, streamsize n);
4
Effects: Assigns up to n characters to successive elements of the array whose first element is designated
by s. The characters assigned are read from the input sequence as if by repeated calls to sbumpc().
Assigning stops when either n characters have been assigned or a call to sbumpc() would return
traits::eof().
5
Returns: The number of characters assigned.305
6
Remarks: Uses traits::eof().
int_type underflow();
7
Remarks: The public members of basic_streambuf call this virtual function only if gptr() is null or
gptr() >= egptr()
8
Returns: traits::to_int_type(c), where c is the first character of the pending sequence, without
moving the input sequence position past it. If the pending sequence is null then the function returns
traits::eof() to indicate failure.
9
The pending sequence of characters is defined as the concatenation of
(9.1)
the empty sequence if gptr() is null, otherwise the characters in [gptr(), egptr()), followed by
(9.2)
some (possibly empty) sequence of characters read from the input sequence.
10
The result character is the first character of the pending sequence if it is non-empty, otherwise the next
character that would be read from the input sequence.
11
The backup sequence is the empty sequence if eback() is null, otherwise the characters in [eback(),
gptr()).
12
Effects: The function sets up the gptr() and egptr() such that if the pending sequence is non-empty,
then egptr() is non-null and the characters in [gptr(), egptr()) are the characters in the pending
sequence, otherwise either gptr() is null or gptr() == egptr().
13
If eback() and gptr() are non-null then the function is not constrained as to their contents, but the
“usual backup condition” is that either
(13.1)
the backup sequence contains at least gptr() - eback() characters, in which case the characters
in [eback(), gptr()) agree with the last gptr() - eback() characters of the backup sequence,
or
(13.2)
the characters in [gptr() - n, gptr()) agree with the backup sequence (where n is the length
of the backup sequence).
14
Default behavior: Returns traits::eof().
int_type uflow();
15
Requires: The constraints are the same as for underflow(), except that the result character shall be
transferred from the pending sequence to the backup sequence, and the pending sequence shall not be
empty before the transfer.
16
Default behavior: Calls underflow(). If underflow() returns traits::eof(), returns traits::eof().
Otherwise, returns the value of traits::to_int_type(*gptr()) and increment the value of the next
pointer for the input sequence.
17
Returns: traits::eof() to indicate failure.
30.6.3.4.4
Putback
[streambuf.virt.pback]
int_type pbackfail(int_type c = traits::eof());
1
Remarks: The public functions of basic_streambuf call this virtual function only when gptr() is
null, gptr() == eback(), or traits::eq(traits::to_char_type(c), gptr()[-1]) returns false.
Other calls shall also satisfy that constraint.
The pending sequence is defined as for underflow(), with the modifications that
305) Classes derived from basic_streambuf can provide more efficient ways to implement xsgetn() and xsputn() by overriding
these definitions from the base class.
§ 30.6.3.4.4
1058
(1.1)
If traits::eq_int_type(c, traits::eof()) returns true, then the input sequence is backed
up one character before the pending sequence is determined.
(1.2)
If traits::eq_int_type(c, traits::eof()) returns false, then c is prepended. Whether the
input sequence is backed up or modified in any other way is unspecified.
2
Postconditions: On return, the constraints of gptr(), eback(), and pptr() are the same as for
underflow().
3
Returns: traits::eof() to indicate failure. Failure may occur because the input sequence could not
be backed up, or if for some other reason the pointers could not be set consistent with the constraints.
pbackfail() is called only when put back has really failed.
4
Returns some value other than traits::eof() to indicate success.
5
Default behavior: Returns traits::eof().
30.6.3.4.5
Put area
[streambuf.virt.put]
streamsize xsputn(const char_type* s, streamsize n);
1
Effects: Writes up to n characters to the output sequence as if by repeated calls to sputc(c). The
characters written are obtained from successive elements of the array whose first element is designated
by s. Writing stops when either n characters have been written or a call to sputc(c) would return
traits::eof(). It is unspecified whether the function calls overflow() when pptr() == epptr()
becomes true or whether it achieves the same effects by other means.
2
Returns: The number of characters written.
int_type overflow(int_type c = traits::eof());
3
Effects: Consumes some initial subsequence of the characters of the pending sequence. The pending
sequence is defined as the concatenation of
(3.1)
the empty sequence if pbase() is null, otherwise the pptr() - pbase() characters beginning at
pbase(), followed by
(3.2)
the empty sequence if traits::eq_int_type(c, traits::eof()) returns true, otherwise the
sequence consisting of c.
4
Remarks: The member functions sputc() and sputn() call this function in case that no room can be
found in the put buffer enough to accommodate the argument character sequence.
5
Requires: Every overriding definition of this virtual function shall obey the following constraints:
(5.1)
The effect of consuming a character on the associated output sequence is specified.306
(5.2)
Let r be the number of characters in the pending sequence not consumed. If r is nonzero then
pbase() and pptr() shall be set so that: pptr() - pbase() == r and the r characters starting
at pbase() are the associated output stream. In case r is zero (all characters of the pending
sequence have been consumed) then either pbase() is set to nullptr, or pbase() and pptr() are
both set to the same non-null value.
(5.3)
The function may fail if either appending some character to the associated output stream fails or
if it is unable to establish pbase() and pptr() according to the above rules.
6
Returns: traits::eof() or throws an exception if the function fails.
Otherwise, returns some value other than traits::eof() to indicate success.307
7
Default behavior: Returns traits::eof().
30.7
Formatting and manipulators
[iostream.format]
30.7.1
Header <istream> synopsis
[istream.syn]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_istream;
306) That is, for each class derived from an instance of basic_streambuf in this Clause (30.8.2, 30.9.2), a specification of how
consuming a character effects the associated output sequence is given. There is no requirement on a program-defined class.
307) Typically, overflow returns c to indicate success, except when traits::eq_int_type(c, traits::eof()) returns true, in
which case it returns traits::not_eof(c).
§ 30.7.1
1059
using istream
= basic_istream<char>;
using wistream = basic_istream<wchar_t>;
template<class charT, class traits = char_traits<charT>>
class basic_iostream;
using iostream
= basic_iostream<char>;
using wiostream = basic_iostream<wchar_t>;
template<class charT, class traits>
basic_istream<charT, traits>& ws(basic_istream<charT, traits>& is);
template<class charT, class traits, class T>
basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>&&
is,
T&& x);
}
30.7.2
Header <ostream> synopsis
[ostream.syn]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_ostream;
using ostream
= basic_ostream<char>;
using wostream = basic_ostream<wchar_t>;
template<class charT, class traits>
basic_ostream<charT, traits>& endl(basic_ostream<charT, traits>& os);
template<class charT, class traits>
basic_ostream<charT, traits>& ends(basic_ostream<charT, traits>& os);
template<class charT, class traits>
basic_ostream<charT, traits>& flush(basic_ostream<charT, traits>& os);
template<class charT, class traits, class T>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&&
os,
const T& x);
}
30.7.3
Header <iomanip> synopsis
[iomanip.syn]
namespace std {
// types T1, T2, ... are unspecified implementation types
T1 resetiosflags(ios_base::fmtflags mask);
T2 setiosflags
(ios_base::fmtflags mask);
T3 setbase(int base);
template<class charT> T4 setfill(charT c);
T5 setprecision(int n);
T6 setw(int n);
template<class moneyT> T7 get_money(moneyT& mon, bool intl = false);
template<class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
template<class charT> T9 get_time(struct tm* tmb, const charT* fmt);
template<class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
template<class charT>
T11 quoted(const charT* s, charT delim = charT(’"’), charT escape = charT(’\\’));
template<class charT, class traits, class Allocator>
T12 quoted(const basic_string<charT, traits, Allocator>& s,
charT delim = charT(’"’), charT escape = charT(’\\’));
template<class charT, class traits, class Allocator>
T13 quoted(basic_string<charT, traits, Allocator>& s,
charT delim = charT(’"’), charT escape = charT(’\\’));
§ 30.7.3
1060
template<class charT, class traits>
T14 quoted(basic_string_view<charT, traits> s,
charT delim = charT(’"’), charT escape = charT(’\\’));
}
30.7.4
Input streams
[input.streams]
1
The header <istream> defines two types and a function signature that control input from a stream buffer
along with a function template that extracts from stream rvalues.
30.7.4.1
Class template basic_istream
[istream]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_istream : virtual public basic_ios<charT, traits> {
public:
// types (inherited from basic_ios (30.5.5))
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;
// 30.7.4.1.1, constructor/destructor
explicit basic_istream(basic_streambuf<charT, traits>* sb);
virtual ~basic_istream();
// 30.7.4.1.3, prefix/suffix
class sentry;
// 30.7.4.2, formatted input
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& (*pf)(basic_istream<charT, traits>&));
basic_istream<charT, traits>&
operator>>(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
basic_istream<charT, traits>&
operator>>(ios_base& (*pf)(ios_base&));
basic_istream<charT, traits>& operator>>(bool& n);
basic_istream<charT, traits>& operator>>(short& n);
basic_istream<charT, traits>& operator>>(unsigned short& n);
basic_istream<charT, traits>& operator>>(int& n);
basic_istream<charT, traits>& operator>>(unsigned int& n);
basic_istream<charT, traits>& operator>>(long& n);
basic_istream<charT, traits>& operator>>(unsigned long& n);
basic_istream<charT, traits>& operator>>(long long& n);
basic_istream<charT, traits>& operator>>(unsigned long long& n);
basic_istream<charT, traits>& operator>>(float& f);
basic_istream<charT, traits>& operator>>(double& f);
basic_istream<charT, traits>& operator>>(long double& f);
basic_istream<charT, traits>& operator>>(void*& p);
basic_istream<charT, traits>& operator>>(basic_streambuf<char_type, traits>* sb);
// 30.7.4.3, unformatted input
streamsize gcount() const;
int_type get();
basic_istream<charT, traits>& get(char_type& c);
basic_istream<charT, traits>& get(char_type* s, streamsize n);
basic_istream<charT, traits>& get(char_type* s, streamsize n, char_type delim);
basic_istream<charT, traits>& get(basic_streambuf<char_type, traits>& sb);
basic_istream<charT, traits>& get(basic_streambuf<char_type, traits>& sb, char_type
delim);
basic_istream<charT, traits>& getline(char_type* s, streamsize n);
basic_istream<charT, traits>& getline(char_type* s, streamsize n, char_type delim);
§
30.7.4.1
1061
basic_istream<charT, traits>& ignore(streamsize n = 1, int_type delim =
traits::eof());
int_type
peek();
basic_istream<charT, traits>& read
(char_type* s, streamsize n);
streamsize
readsome(char_type* s, streamsize n);
basic_istream<charT, traits>& putback(char_type c);
basic_istream<charT, traits>& unget();
int sync();
pos_type tellg();
basic_istream<charT, traits>& seekg(pos_type);
basic_istream<charT, traits>& seekg(off_type, ios_base::seekdir);
protected:
// 30.7.4.1.1, copy/move constructor
basic_istream(const basic_istream& rhs) = delete;
basic_istream(basic_istream&& rhs);
// 30.7.4.1.2, assign and swap
basic_istream& operator=(const basic_istream& rhs) = delete;
basic_istream& operator=(basic_istream&& rhs);
void swap(basic_istream& rhs);
};
// 30.7.4.2.3, character extraction templates
template<class charT, class traits>
basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>&,
charT&);
template<class traits>
basic_istream<char, traits>& operator>>(basic_istream<char, traits>&, unsigned char&);
template<class traits>
basic_istream<char, traits>& operator>>(basic_istream<char, traits>&, signed char&);
template<class charT, class traits>
basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>&, charT*);
template<class traits>
basic_istream<char, traits>& operator>>(basic_istream<char, traits>&, unsigned char*);
template<class traits>
basic_istream<char, traits>& operator>>(basic_istream<char, traits>&, signed char*);
}
1
The class template basic_istream defines a number of member function signatures that assist in reading
and interpreting input from sequences controlled by a stream buffer.
2
Two groups of member function signatures share common properties: the formatted input functions (or
extractors) and the unformatted input functions. Both groups of input functions are described as if they
obtain (or extract) input characters by calling rdbuf()->sbumpc() or rdbuf()->sgetc(). They may use
other public members of istream.
3
If rdbuf()->sbumpc() or rdbuf()->sgetc() returns traits::eof(), then the input function, except as
explicitly noted otherwise, completes its actions and does setstate(eofbit), which may throw ios_-
base::failure (30.5.5.4), before returning.
4
If one of these called functions throws an exception, then unless explicitly noted otherwise, the input function
sets badbit in error state. If badbit is on in exceptions(), the input function rethrows the exception
without completing its actions, otherwise it does not throw anything and proceeds as if the called function
had returned a failure indication.
30.7.4.1.1
basic_istream constructors
[istream.cons]
explicit basic_istream(basic_streambuf<charT, traits>* sb);
1
Effects: Constructs an object of class basic_istream, initializing the base class subobject with
basic_ios::init(sb) (30.5.5.2).
2
Postconditions: gcount() == 0.
§ 30.7.4.1.1
1062
basic_istream(basic_istream&& rhs);
3
Effects: Move constructs from the rvalue rhs. This is accomplished by default constructing the base
class, copying the gcount() from rhs, calling basic_ios<charT, traits>::move(rhs) to initialize
the base class, and setting the gcount() for rhs to 0.
virtual ~basic_istream();
4
Effects: Destroys an object of class basic_istream.
5
Remarks: Does not perform any operations of rdbuf().
30.7.4.1.2
Class basic_istream assign and swap
[istream.assign]
basic_istream& operator=(basic_istream&& rhs);
1
Effects: As if by swap(rhs).
2
Returns: *this.
void swap(basic_istream& rhs);
3
Effects: Calls basic_ios<charT, traits>::swap(rhs). Exchanges the values returned by gcount()
and rhs.gcount().
30.7.4.1.3
Class basic_istream::sentry
[istream::sentry]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_istream<charT, traits>::sentry {
using traits_type = traits;
bool ok_; // exposition only
public:
explicit sentry(basic_istream<charT, traits>& is, bool noskipws = false);
~sentry();
explicit operator bool() const { return ok_; }
sentry(const sentry&) = delete;
sentry& operator=(const sentry&) = delete;
};
}
1
The class sentry defines a class that is responsible for doing exception safe prefix and suffix operations.
explicit sentry(basic_istream<charT, traits>& is, bool noskipws = false);
2
Effects: If is.good() is false, calls is.setstate(failbit). Otherwise, prepares for formatted or
unformatted input. First, if is.tie() is not a null pointer, the function calls is.tie()->flush()
to synchronize the output sequence with any associated external C stream. Except that this call
can be suppressed if the put area of is.tie() is empty. Further an implementation is allowed to
defer the call to flush until a call of is.rdbuf()->underflow() occurs. If no such call occurs before
the sentry object is destroyed, the call to flush may be eliminated entirely.308 If noskipws is zero
and is.flags() & ios_base::skipws is nonzero, the function extracts and discards each character
as long as the next available input character c is a whitespace character. If is.rdbuf()->sbumpc()
or is.rdbuf()->sgetc() returns traits::eof(), the function calls setstate(failbit | eofbit)
(which may throw ios_base::failure).
3
Remarks: The constructor
explicit sentry(basic_istream<charT, traits>& is, bool noskipws = false)
uses the currently imbued locale in is, to determine whether the next input character is whitespace or
not.
4
To decide if the character c is a whitespace character, the constructor performs as if it executes the
following code fragment:
const ctype<charT>& ctype = use_facet<ctype<charT>>(is.getloc());
if (ctype.is(ctype.space, c) != 0)
// c is a whitespace character.
308) This will be possible only in functions that are part of the library. The semantics of the constructor used in user code is as
specified.
§ 30.7.4.1.3
1063
5
If, after any preparation is completed, is.good() is true, ok_ != false otherwise, ok_ == false.
During preparation, the constructor may call setstate(failbit) (which may throw ios_base::
failure (30.5.5.4))309
~sentry();
6
Effects: None.
explicit operator bool() const;
7
Effects: Returns ok_.
30.7.4.2
Formatted input functions
[istream.formatted]
30.7.4.2.1
Common requirements
[istream.formatted.reqmts]
1
Each formatted input function begins execution by constructing an object of class sentry with the noskipws
(second) argument false. If the sentry object returns true, when converted to a value of type bool, the
function endeavors to obtain the requested input. If an exception is thrown during input then ios::badbit
is turned on310 in *this’s error state. If (exceptions()&badbit) != 0 then the exception is rethrown. In
any case, the formatted input function destroys the sentry object. If no exception has been thrown, it
returns *this.
30.7.4.2.2
Arithmetic extractors
[istream.formatted.arithmetic]
operator>>(unsigned short& val);
operator>>(unsigned int& val);
operator>>(long& val);
operator>>(unsigned long& val);
operator>>(long long& val);
operator>>(unsigned long long& val);
operator>>(float& val);
operator>>(double& val);
operator>>(long double& val);
operator>>(bool& val);
operator>>(void*& val);
1
As in the case of the inserters, these extractors depend on the locale’s num_get<> (25.4.2.1) object
to perform parsing the input stream data. These extractors behave as formatted input functions (as
described in 30.7.4.2.1). After a sentry object is constructed, the conversion occurs as if performed by
the following code fragment:
using numget = num_get<charT, istreambuf_iterator<charT, traits>>;
iostate err = iostate::goodbit;
use_facet<numget>(loc).get(*this, 0, *this, err, val);
setstate(err);
In the above fragment, loc stands for the private member of the basic_ios class. [Note: The first
argument provides an object of the istreambuf_iterator class which is an iterator pointed to an
input stream. It bypasses istreams and uses streambufs directly.
— end note ] Class locale relies on
this type as its interface to istream, so that it does not need to depend directly on istream.
operator>>(short& val);
2
The conversion occurs as if performed by the following code fragment (using the same notation as for
the preceding code fragment):
using numget = num_get<charT, istreambuf_iterator<charT, traits>>;
iostate err = ios_base::goodbit;
long lval;
use_facet<numget>(loc).get(*this, 0, *this, err, lval);
if (lval < numeric_limits<short>::min()) {
err |= ios_base::failbit;
val = numeric_limits<short>::min();
} else if (numeric_limits<short>::max() < lval) {
err |= ios_base::failbit;
309) The sentry constructor and destructor can also perform additional implementation-dependent operations.
310) This is done without causing an ios::failure to be thrown.
§ 30.7.4.2.2
1064
val = numeric_limits<short>::max();
} else
val = static_cast<short>(lval);
setstate(err);
operator>>(int& val);
3
The conversion occurs as if performed by the following code fragment (using the same notation as for
the preceding code fragment):
using numget = num_get<charT, istreambuf_iterator<charT, traits>>;
iostate err = ios_base::goodbit;
long lval;
use_facet<numget>(loc).get(*this, 0, *this, err, lval);
if (lval < numeric_limits<int>::min()) {
err |= ios_base::failbit;
val = numeric_limits<int>::min();
} else if (numeric_limits<int>::max() < lval) {
err |= ios_base::failbit;
val = numeric_limits<int>::max();
} else
val = static_cast<int>(lval);
setstate(err);
30.7.4.2.3
basic_istream::operator>>
[istream.extractors]
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& (*pf)(basic_istream<charT, traits>&));
1
Effects: None. This extractor does not behave as a formatted input function (as described in 30.7.4.2.1).
2
Returns: pf(*this).311
basic_istream<charT, traits>&
operator>>(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
3
Effects: Calls pf(*this). This extractor does not behave as a formatted input function (as described
in 30.7.4.2.1).
4
Returns: *this.
basic_istream<charT, traits>& operator>>(ios_base& (*pf)(ios_base&));
5
Effects: Calls pf(*this).312 This extractor does not behave as a formatted input function (as described
in 30.7.4.2.1).
6
Returns: *this.
template<class charT, class traits>
basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>& in, charT* s);
template<class traits>
basic_istream<char, traits>& operator>>(basic_istream<char, traits>& in, unsigned char* s);
template<class traits>
basic_istream<char, traits>& operator>>(basic_istream<char, traits>& in, signed char* s);
7
Effects: Behaves like a formatted input member (as described in 30.7.4.2.1) of in. After a sentry
object is constructed, operator>> extracts characters and stores them into successive locations of an
array whose first element is designated by s. If width() is greater than zero, n is width(). Otherwise
n is the number of elements of the largest array of char_type that can store a terminating charT(). n
is the maximum number of characters stored.
8
Characters are extracted and stored until any of the following occurs:
(8.1)
n-1 characters are stored;
(8.2)
end of file occurs on the input sequence;
(8.3)
letting ct be use_facet<ctype<charT>>(in.getloc()), ct.is(ct.space, c) is true.
311) See, for example, the function signature ws(basic_istream&) (30.7.4.4).
312) See, for example, the function signature dec(ios_base&) (30.5.6.3).
§ 30.7.4.2.3
1065
9
operator>> then stores a null byte (charT()) in the next position, which may be the first position if
no characters were extracted. operator>> then calls width(0).
10
If the function extracted no characters, it calls setstate(failbit), which may throw ios_base::
failure (30.5.5.4).
11
Returns: in.
template<class charT, class traits>
basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>& in, charT& c);
template<class traits>
basic_istream<char, traits>& operator>>(basic_istream<char, traits>& in, unsigned char& c);
template<class traits>
basic_istream<char, traits>& operator>>(basic_istream<char, traits>& in, signed char& c);
12
Effects: Behaves like a formatted input member (as described in 30.7.4.2.1) of in. After a sentry
object is constructed a character is extracted from in, if one is available, and stored in c. Otherwise,
the function calls in.setstate(failbit).
13
Returns: in.
basic_istream<charT, traits>& operator>>(basic_streambuf<charT, traits>* sb);
14
Effects: Behaves as an unformatted input function (30.7.4.3). If sb is null, calls setstate(fail-
bit), which may throw ios_base::failure (30.5.5.4). After a sentry object is constructed, extracts
characters from *this and inserts them in the output sequence controlled by sb. Characters are
extracted and inserted until any of the following occurs:
(14.1)
end-of-file occurs on the input sequence;
(14.2)
inserting in the output sequence fails (in which case the character to be inserted is not extracted);
(14.3)
an exception occurs (in which case the exception is caught).
15
If the function inserts no characters, it calls setstate(failbit), which may throw ios_base::
failure (30.5.5.4). If it inserted no characters because it caught an exception thrown while extracting
characters from *this and failbit is on in exceptions() (30.5.5.4), then the caught exception is
rethrown.
16
Returns: *this.
30.7.4.3
Unformatted input functions
[istream.unformatted]
1
Each unformatted input function begins execution by constructing an object of class sentry with the default
argument noskipws (second) argument true. If the sentry object returns true, when converted to a value
of type bool, the function endeavors to obtain the requested input. Otherwise, if the sentry constructor exits
by throwing an exception or if the sentry object returns false, when converted to a value of type bool, the
function returns without attempting to obtain any input. In either case the number of extracted characters is
set to 0; unformatted input functions taking a character array of nonzero size as an argument shall also store
a null character (using charT()) in the first location of the array. If an exception is thrown during input then
ios::badbit is turned on313 in *this’s error state. (Exceptions thrown from basic_ios<>::clear() are
not caught or rethrown.) If (exceptions()&badbit) != 0 then the exception is rethrown. It also counts
the number of characters extracted. If no exception has been thrown it ends by storing the count in a member
object and returning the value specified. In any event the sentry object is destroyed before leaving the
unformatted input function.
streamsize gcount() const;
2
Effects: None. This member function does not behave as an unformatted input function (as described
above).
3
Returns: The number of characters extracted by the last unformatted input member function called for
the object.
313) This is done without causing an ios::failure to be thrown.
§ 30.7.4.3
1066
int_type get();
4
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, extracts a character c, if one is available. Otherwise, the function calls setstate(failbit),
which may throw ios_base::failure (30.5.5.4),
5
Returns: c if available, otherwise traits::eof().
basic_istream<charT, traits>& get(char_type& c);
6
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, extracts a character, if one is available, and assigns it to c.314
Otherwise, the function calls
setstate(failbit) (which may throw ios_base::failure (30.5.5.4)).
7
Returns: *this.
basic_istream<charT, traits>& get(char_type* s, streamsize n, char_type delim);
8
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, extracts characters and stores them into successive locations of an array whose first element is
designated by s.315 Characters are extracted and stored until any of the following occurs:
(8.1)
n is less than one or n - 1 characters are stored;
(8.2)
end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit));
(8.3)
traits::eq(c, delim) for the next available input character c (in which case c is not extracted).
9
If the function stores no characters, it calls setstate(failbit) (which may throw ios_base::failure
(30.5.5.4)). In any case, if n is greater than zero it then stores a null character into the next successive
location of the array.
10
Returns: *this.
basic_istream<charT, traits>& get(char_type* s, streamsize n);
11
Effects: Calls get(s, n, widen(’\n’)).
12
Returns: Value returned by the call.
basic_istream<charT, traits>& get(basic_streambuf<char_type, traits>& sb, char_type delim);
13
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, extracts characters and inserts them in the output sequence controlled by sb. Characters are
extracted and inserted until any of the following occurs:
(13.1)
end-of-file occurs on the input sequence;
(13.2)
inserting in the output sequence fails (in which case the character to be inserted is not extracted);
(13.3)
traits::eq(c, delim) for the next available input character c (in which case c is not extracted);
(13.4)
an exception occurs (in which case, the exception is caught but not rethrown).
14
If the function inserts no characters, it calls setstate(failbit), which may throw ios_base::
failure (30.5.5.4).
15
Returns: *this.
basic_istream<charT, traits>& get(basic_streambuf<char_type, traits>& sb);
16
Effects: Calls get(sb, widen(’\n’)).
17
Returns: Value returned by the call.
basic_istream<charT, traits>& getline(char_type* s, streamsize n, char_type delim);
18
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, extracts characters and stores them into successive locations of an array whose first element is
designated by s.316 Characters are extracted and stored until one of the following occurs:
1. end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit));
314) Note that this function is not overloaded on types signed char and unsigned char.
315) Note that this function is not overloaded on types signed char and unsigned char.
316) Note that this function is not overloaded on types signed char and unsigned char.
§ 30.7.4.3
1067
2. traits::eq(c, delim) for the next available input character c (in which case the input character
is extracted but not stored);317
3. n is less than one or n - 1 characters are stored (in which case the function calls setstate(
failbit)).
19
These conditions are tested in the order shown.318
20
If the function extracts no characters, it calls setstate(failbit) (which may throw ios_base::
failure (30.5.5.4)).319
21
In any case, if n is greater than zero, it then stores a null character (using charT()) into the next
successive location of the array.
22
Returns: *this.
23
[ Example:
#include <iostream>
int main() {
using namespace std;
const int line_buffer_size = 100;
char buffer[line_buffer_size];
int line_number = 0;
while (cin.getline(buffer, line_buffer_size, ’\n’) || cin.gcount())
{
int count = cin.gcount();
if (cin.eof())
cout << "Partial final line";
// cin.fail() is false
else if (cin.fail()) {
cout << "Partial long line";
cin.clear(cin.rdstate() & ~ios_base::failbit);
} else {
count--;
// Don’t include newline in count
cout << "Line " << ++line_number;
}
cout << " (" << count << " chars): " << buffer << endl;
}
}
— end example ]
basic_istream<charT, traits>& getline(char_type* s, streamsize n);
24
Returns: getline(s, n, widen(’\n’))
basic_istream<charT, traits>& ignore(streamsize n = 1, int_type delim = traits::eof());
25
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, extracts characters and discards them. Characters are extracted until any of the following
occurs:
(25.1)
n != numeric_limits<streamsize>::max() (21.3.4) and n characters have been extracted so
far
(25.2)
end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit),
which may throw ios_base::failure (30.5.5.4));
(25.3)
traits::eq_int_type(traits::to_int_type(c), delim) for the next available input character
c (in which case c is extracted).
26
Remarks: The last condition will never occur if traits::eq_int_type(delim, traits::eof()).
27
Returns: *this.
317) Since the final input character is “extracted”, it is counted in the gcount(), even though it is not stored.
318) This allows an input line which exactly fills the buffer, without setting failbit. This is different behavior than the historical
AT&T implementation.
319) This implies an empty input line will not cause failbit to be set.
§ 30.7.4.3
1068
int_type peek();
28
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, reads but does not extract the current input character.
29
Returns: traits::eof() if good() is false. Otherwise, returns rdbuf()->sgetc().
basic_istream<charT, traits>& read(char_type* s, streamsize n);
30
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, if !good() calls setstate(failbit) which may throw an exception, and return. Otherwise
extracts characters and stores them into successive locations of an array whose first element is designated
by s.320 Characters are extracted and stored until either of the following occurs:
(30.1)
n characters are stored;
(30.2)
end-of-file occurs on the input sequence (in which case the function calls setstate(failbit |
eofbit), which may throw ios_base::failure (30.5.5.4)).
31
Returns: *this.
streamsize readsome(char_type* s, streamsize n);
32
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry
object, if
!good() calls setstate(failbit) which may throw an exception, and return. Other-
wise extracts characters and stores them into successive locations of an array whose first element
is designated by s. If rdbuf()->in_avail() == -1, calls setstate(eofbit) (which may throw
ios_base::failure (30.5.5.4)), and extracts no characters;
(32.1)
If rdbuf()->in_avail() == 0, extracts no characters
(32.2)
If rdbuf()->in_avail() > 0, extracts min(rdbuf()->in_avail(), n)).
33
Returns: The number of characters extracted.
basic_istream<charT, traits>& putback(char_type c);
34
Effects: Behaves as an unformatted input function (as described above), except that the function
first clears eofbit. After constructing a sentry object, if !good() calls setstate(failbit) which
may throw an exception, and return. If rdbuf() is not null, calls rdbuf->sputbackc(). If rdbuf()
is null, or if sputbackc() returns traits::eof(), calls setstate(badbit) (which may throw ios_-
base::failure (30.5.5.4)). [ Note: This function extracts no characters, so the value returned by the
next call to gcount() is 0.
— end note ]
35
Returns: *this.
basic_istream<charT, traits>& unget();
36
Effects: Behaves as an unformatted input function (as described above), except that the function
first clears eofbit. After constructing a sentry object, if !good() calls setstate(failbit) which
may throw an exception, and return. If rdbuf() is not null, calls rdbuf()->sungetc(). If rdbuf()
is null, or if sungetc() returns traits::eof(), calls setstate(badbit) (which may throw ios_-
base::failure (30.5.5.4)). [ Note: This function extracts no characters, so the value returned by the
next call to gcount() is 0.
— end note ]
37
Returns: *this.
int sync();
38
Effects: Behaves as an unformatted input function (as described above), except that it does not count
the number of characters extracted and does not affect the value returned by subsequent calls to
gcount(). After constructing a sentry object, if rdbuf() is a null pointer, returns -1. Otherwise,
calls rdbuf()->pubsync() and, if that function returns -1 calls setstate(badbit) (which may throw
ios_base::failure (30.5.5.4), and returns -1. Otherwise, returns zero.
320) Note that this function is not overloaded on types signed char and unsigned char.
§ 30.7.4.3
1069
pos_type tellg();
39
Effects: Behaves as an unformatted input function (as described above), except that it does not count
the number of characters extracted and does not affect the value returned by subsequent calls to
gcount().
40
Returns: After constructing a sentry object, if fail() != false, returns pos_type(-1) to indicate
failure. Otherwise, returns rdbuf()->pubseekoff(0, cur, in).
basic_istream<charT, traits>& seekg(pos_type pos);
41
Effects: Behaves as an unformatted input function (as described above), except that the function
first clears eofbit, it does not count the number of characters extracted, and it does not affect the
value returned by subsequent calls to gcount(). After constructing a sentry object, if fail() !=
true, executes rdbuf()->pubseekpos(pos, ios_base::in). In case of failure, the function calls
setstate(failbit) (which may throw ios_base::failure).
42
Returns: *this.
basic_istream<charT, traits>& seekg(off_type off, ios_base::seekdir dir);
43
Effects: Behaves as an unformatted input function (as described above), except that the function
first clears eofbit, does not count the number of characters extracted, and does not affect the value
returned by subsequent calls to gcount(). After constructing a sentry object, if fail() != true,
executes rdbuf()->pubseekoff(off, dir, ios_base::in). In case of failure, the function calls
setstate(failbit) (which may throw ios_base::failure).
44
Returns: *this.
30.7.4.4
Standard basic_istream manipulators
[istream.manip]
template<class charT, class traits>
basic_istream<charT, traits>& ws(basic_istream<charT, traits>& is);
1
Effects: Behaves as an unformatted input function (30.7.4.3), except that it does not count the number
of characters extracted and does not affect the value returned by subsequent calls to is.gcount(). After
constructing a sentry object extracts characters as long as the next available character c is whitespace
or until there are no more characters in the sequence. Whitespace characters are distinguished with the
same criterion as used by sentry::sentry (30.7.4.1.3). If ws stops extracting characters because there
are no more available it sets eofbit, but not failbit.
2
Returns: is.
30.7.4.5
Rvalue stream extraction
[istream.rvalue]
template<class charT, class traits, class T>
basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>&& is, T&& x);
1
Effects: Equivalent to:
is >> std::forward<T>(x);
return is;
2
Remarks: This function shall not participate in overload resolution unless the expression is >>
std::forward<T>(x) is well-formed.
30.7.4.6
Class template basic_iostream
[iostreamclass]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_iostream
: public basic_istream<charT, traits>,
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;
§ 30.7.4.6
1070
// 30.7.4.6.1, constructor
explicit basic_iostream(basic_streambuf<charT, traits>* sb);
// 30.7.4.6.2, destructor
virtual ~basic_iostream();
protected:
// 30.7.4.6.1, constructor
basic_iostream(const basic_iostream& rhs) = delete;
basic_iostream(basic_iostream&& rhs);
// 30.7.4.6.3, assign and swap
basic_iostream& operator=(const basic_iostream& rhs) = delete;
basic_iostream& operator=(basic_iostream&& rhs);
void swap(basic_iostream& rhs);
};
}
1
The class template basic_iostream inherits a number of functions that allow reading input and writing
output to sequences controlled by a stream buffer.
30.7.4.6.1
basic_iostream constructors
[iostream.cons]
explicit basic_iostream(basic_streambuf<charT, traits>* sb);
1
Effects: Constructs an object of class basic_iostream, initializing the base class subobjects with
basic_istream<charT, traits>(sb) (30.7.4.1) and basic_ostream<charT, traits>(sb) (30.7.5.1).
2
Postconditions: rdbuf() == sb and gcount() == 0.
basic_iostream(basic_iostream&& rhs);
3
Effects: Move constructs from the rvalue rhs by constructing the basic_istream base class with
move(rhs).
30.7.4.6.2
basic_iostream destructor
[iostream.dest]
virtual ~basic_iostream();
1
Effects: Destroys an object of class basic_iostream.
2
Remarks: Does not perform any operations on rdbuf().
30.7.4.6.3
basic_iostream assign and swap
[iostream.assign]
basic_iostream& operator=(basic_iostream&& rhs);
1
Effects: As if by swap(rhs).
void swap(basic_iostream& rhs);
2
Effects: Calls basic_istream<charT, traits>::swap(rhs).
30.7.5
Output streams
[output.streams]
1
The header <ostream> defines a type and several function signatures that control output to a stream buffer
along with a function template that inserts into stream rvalues.
30.7.5.1
Class template basic_ostream
[ostream]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_ostream : virtual public basic_ios<charT, traits> {
public:
// types (inherited from basic_ios (30.5.5))
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;
§ 30.7.5.1
1071

 

 

 

 

 

 

 

Content      ..     34      35      36      37     ..