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

 

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

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     35      36      37      38     ..

 

 

 

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

 

 

// 30.7.5.1.1, constructor/destructor
explicit basic_ostream(basic_streambuf<char_type, traits>* sb);
virtual ~basic_ostream();
// 30.7.5.1.3, prefix/suffix
class sentry;
// 30.7.5.2, formatted output
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& (*pf)(basic_ostream<charT, traits>&));
basic_ostream<charT, traits>&
operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
basic_ostream<charT, traits>&
operator<<(ios_base& (*pf)(ios_base&));
basic_ostream<charT, traits>& operator<<(bool n);
basic_ostream<charT, traits>& operator<<(short n);
basic_ostream<charT, traits>& operator<<(unsigned short n);
basic_ostream<charT, traits>& operator<<(int n);
basic_ostream<charT, traits>& operator<<(unsigned int n);
basic_ostream<charT, traits>& operator<<(long n);
basic_ostream<charT, traits>& operator<<(unsigned long n);
basic_ostream<charT, traits>& operator<<(long long n);
basic_ostream<charT, traits>& operator<<(unsigned long long n);
basic_ostream<charT, traits>& operator<<(float f);
basic_ostream<charT, traits>& operator<<(double f);
basic_ostream<charT, traits>& operator<<(long double f);
basic_ostream<charT, traits>& operator<<(const void* p);
basic_ostream<charT, traits>& operator<<(nullptr_t);
basic_ostream<charT, traits>& operator<<(basic_streambuf<char_type, traits>*
sb);
// 30.7.5.3, unformatted output
basic_ostream<charT, traits>& put(char_type c);
basic_ostream<charT, traits>& write(const char_type* s, streamsize n);
basic_ostream<charT, traits>& flush();
// 30.7.5.1.4, seeks
pos_type tellp();
basic_ostream<charT, traits>& seekp(pos_type);
basic_ostream<charT, traits>& seekp(off_type, ios_base::seekdir);
protected:
// 30.7.5.1.1, copy/move constructor
basic_ostream(const basic_ostream& rhs) = delete;
basic_ostream(basic_ostream&& rhs);
// 30.7.5.1.2, assign and swap
basic_ostream& operator=(const basic_ostream& rhs) = delete;
basic_ostream& operator=(basic_ostream&& rhs);
void swap(basic_ostream& rhs);
};
// 30.7.5.2.4, character inserters
template<class charT, class traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, charT);
template<class charT, class traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, char);
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char);
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, signed char);
§
30.7.5.1
1072
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, unsigned char);
template<class charT, class traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, const charT*);
template<class charT, class traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, const char*);
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char*);
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const signed char*);
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const unsigned char*);
}
1
The class template basic_ostream defines a number of member function signatures that assist in formatting
and writing output to output sequences controlled by a stream buffer.
2
Two groups of member function signatures share common properties: the formatted output functions (or
inserters) and the unformatted output functions. Both groups of output functions generate (or insert) output
characters by actions equivalent to calling rdbuf()->sputc(int_type). They may use other public members
of basic_ostream except that they shall not invoke any virtual members of rdbuf() except overflow(),
xsputn(), and sync().
3
If one of these called functions throws an exception, then unless explicitly noted otherwise the output function
sets badbit in error state. If badbit is on in exceptions(), the output function rethrows the exception
without completing its actions, otherwise it does not throw anything and treat as an error.
30.7.5.1.1
basic_ostream constructors
[ostream.cons]
explicit basic_ostream(basic_streambuf<charT, traits>* sb);
1
Effects: Constructs an object of class basic_ostream, initializing the base class subobject with
basic_ios<charT, traits>::init(sb) (30.5.5.2).
2
Postconditions: rdbuf() == sb.
basic_ostream(basic_ostream&& rhs);
3
Effects: Move constructs from the rvalue rhs. This is accomplished by default constructing the base
class and calling basic_ios<charT, traits>::move(rhs) to initialize the base class.
virtual ~basic_ostream();
4
Effects: Destroys an object of class basic_ostream.
5
Remarks: Does not perform any operations on rdbuf().
30.7.5.1.2
Class basic_ostream assign and swap
[ostream.assign]
basic_ostream& operator=(basic_ostream&& rhs);
1
Effects: As if by swap(rhs).
2
Returns: *this.
void swap(basic_ostream& rhs);
3
Effects: Calls basic_ios<charT, traits>::swap(rhs).
30.7.5.1.3
Class basic_ostream::sentry
[ostream::sentry]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_ostream<charT, traits>::sentry {
bool ok_; // exposition only
public:
explicit sentry(basic_ostream<charT, traits>& os);
~sentry();
explicit operator bool() const { return ok_; }
§ 30.7.5.1.3
1073
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_ostream<charT, traits>& os);
2
If os.good() is nonzero, prepares for formatted or unformatted output. If os.tie() is not a null
pointer, calls os.tie()->flush().321
3
If, after any preparation is completed, os.good() is true, ok_ == true otherwise, ok_ == false.
During preparation, the constructor may call setstate(failbit) (which may throw ios_base::
failure (30.5.5.4))322
~sentry();
4
If
(os.flags() & ios_base::unitbuf) && !uncaught_exceptions() && os.good() is true, calls
os.rdbuf()->pubsync(). If that function returns -1, sets badbit in os.rdstate() without propagating
an exception.
explicit operator bool() const;
5
Effects: Returns ok_.
30.7.5.1.4
basic_ostream seek members
[ostream.seeks]
1
Each seek member function begins execution by constructing an object of class sentry. It returns by
destroying the sentry object.
pos_type tellp();
2
Returns: If fail() != false, returns pos_type(-1) to indicate failure. Otherwise, returns rdbuf()->
pubseekoff(0, cur, out).
basic_ostream<charT, traits>& seekp(pos_type pos);
3
Effects: If fail() != true, executes rdbuf()->pubseekpos(pos, ios_base::out). In case of failure,
the function calls setstate(failbit) (which may throw ios_base::failure).
4
Returns: *this.
basic_ostream<charT, traits>& seekp(off_type off, ios_base::seekdir dir);
5
Effects: If fail() != true, executes rdbuf()->pubseekoff(off, dir, ios_base::out). In case of
failure, the function calls setstate(failbit) (which may throw ios_base::failure).
6
Returns: *this.
30.7.5.2
Formatted output functions
[ostream.formatted]
30.7.5.2.1
Common requirements
[ostream.formatted.reqmts]
1
Each formatted output function begins execution by constructing an object of class sentry. If this object
returns true when converted to a value of type bool, the function endeavors to generate the requested
output. If the generation fails, then the formatted output function does setstate(ios_base::failbit),
which might throw an exception. If an exception is thrown during output, then ios::badbit is turned on323
in *this’s error state. If (exceptions()&badbit) != 0 then the exception is rethrown. Whether or not
an exception is thrown, the sentry object is destroyed before leaving the formatted output function. If no
exception is thrown, the result of the formatted output function is *this.
2
The descriptions of the individual formatted output functions describe how they perform output and do not
mention the sentry object.
3
If a formatted output function of a stream os determines padding, it does so as follows. Given a charT
character sequence seq where charT is the character type of the stream, if the length of seq is less than
os.width(), then enough copies of os.fill() are added to this sequence as necessary to pad to a width of
321) The call os.tie()->flush() does not necessarily occur if the function can determine that no synchronization is necessary.
322) The sentry constructor and destructor can also perform additional implementation-dependent operations.
323) without causing an ios::failure to be thrown.
§ 30.7.5.2.1
1074
os.width() characters. If (os.flags() & ios_base::adjustfield) == ios_base::left is true, the fill
characters are placed after the character sequence; otherwise, they are placed before the character sequence.
30.7.5.2.2
Arithmetic inserters
[ostream.inserters.arithmetic]
operator<<(bool val);
operator<<(short val);
operator<<(unsigned short val);
operator<<(int 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<<(const void* val);
1
Effects: The classes num_get<> and num_put<> handle locale-dependent numeric formatting and parsing.
These inserter functions use the imbued locale value to perform numeric formatting. When val is of
type bool, long, unsigned long, long long, unsigned long long, double, long double, or const
void*, the formatting conversion occurs as if it performed the following code fragment:
bool failed = use_facet<
num_put<charT, ostreambuf_iterator<charT, traits>>
>(getloc()).put(*this, *this, fill(), val).failed();
When val is of type short the formatting conversion occurs as if it performed the following code
fragment:
ios_base::fmtflags baseflags = ios_base::flags() & ios_base::basefield;
bool failed = use_facet<
num_put<charT, ostreambuf_iterator<charT, traits>>
>(getloc()).put(*this, *this, fill(),
baseflags == ios_base::oct || baseflags == ios_base::hex
? static_cast<long>(static_cast<unsigned short>(val))
: static_cast<long>(val)).failed();
When val is of type int the formatting conversion occurs as if it performed the following code
fragment:
ios_base::fmtflags baseflags = ios_base::flags() & ios_base::basefield;
bool failed = use_facet<
num_put<charT, ostreambuf_iterator<charT, traits>>
>(getloc()).put(*this, *this, fill(),
baseflags == ios_base::oct || baseflags == ios_base::hex
? static_cast<long>(static_cast<unsigned int>(val))
: static_cast<long>(val)).failed();
When val is of type unsigned short or unsigned int the formatting conversion occurs as if it
performed the following code fragment:
bool failed = use_facet<
num_put<charT, ostreambuf_iterator<charT, traits>>
>(getloc()).put(*this, *this, fill(),
static_cast<unsigned long>(val)).failed();
When val is of type float the formatting conversion occurs as if it performed the following code
fragment:
bool failed = use_facet<
num_put<charT, ostreambuf_iterator<charT, traits>>
>(getloc()).put(*this, *this, fill(),
static_cast<double>(val)).failed();
2
The first argument provides an object of the ostreambuf_iterator<> class which is an iterator for
class basic_ostream<>. It bypasses ostreams and uses streambufs directly. Class locale relies on
these types as its interface to iostreams, since for flexibility it has been abstracted away from direct
§
30.7.5.2.2
1075
dependence on ostream. The second parameter is a reference to the base class subobject of type
ios_base. It provides formatting specifications such as field width, and a locale from which to obtain
other facets. If failed is true then does setstate(badbit), which may throw an exception, and
returns.
3
Returns: *this.
30.7.5.2.3
basic_ostream::operator<<
[ostream.inserters]
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& (*pf)(basic_ostream<charT, traits>&));
1
Effects: None. Does not behave as a formatted output function (as described in 30.7.5.2.1).
2
Returns: pf(*this).324
basic_ostream<charT, traits>&
operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
3
Effects: Calls pf(*this). This inserter does not behave as a formatted output function (as described
in 30.7.5.2.1).
4
Returns: *this.325
basic_ostream<charT, traits>& operator<<(ios_base& (*pf)(ios_base&));
5
Effects: Calls pf(*this). This inserter does not behave as a formatted output function (as described
in 30.7.5.2.1).
6
Returns: *this.
basic_ostream<charT, traits>& operator<<(basic_streambuf<charT, traits>* sb);
7
Effects: Behaves as an unformatted output function (30.7.5.3). After the sentry object is constructed,
if sb is null calls setstate(badbit) (which may throw ios_base::failure).
8
Gets characters from sb and inserts them in *this. Characters are read from sb and inserted until any
of the following occurs:
(8.1)
end-of-file occurs on the input sequence;
(8.2)
inserting in the output sequence fails (in which case the character to be inserted is not extracted);
(8.3)
an exception occurs while getting a character from sb.
9
If the function inserts no characters, it calls setstate(failbit) (which may throw ios_base::
failure (30.5.5.4)). If an exception was thrown while extracting a character, the function sets failbit
in error state, and if failbit is on in exceptions() the caught exception is rethrown.
10
Returns: *this.
basic_ostream<charT, traits>& operator<<(nullptr_t);
11
Effects: Equivalent to:
return *this << s;
where s is an implementation-defined NTCTS (20.3.16).
30.7.5.2.4
Character inserter function templates
[ostream.inserters.character]
template<class charT, class traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, charT c);
template<class charT, class traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, char c);
// specialization
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, char c);
// signed and unsigned
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, signed char c);
324) See, for example, the function signature endl(basic_ostream&) (30.7.5.4).
325) See, for example, the function signature dec(ios_base&) (30.5.6.3).
§ 30.7.5.2.4
1076
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, unsigned char c);
1
Effects: Behaves as a formatted output function (30.7.5.2.1) of out. Constructs a character sequence
seq. If c has type char and the character type of the stream is not char, then seq consists of
out.widen(c); otherwise seq consists of c. Determines padding for seq as described in 30.7.5.2.1.
Inserts seq into out. Calls os.width(0).
2
Returns: out.
template<class charT, class traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, const charT* s);
template<class charT, class traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, const char* s);
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, const char* s);
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out, const signed char* s);
template<class traits>
basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>& out,
const unsigned char* s);
3
Requires: s shall not be a null pointer.
4
Effects: Behaves like a formatted inserter (as described in 30.7.5.2.1) of out. Creates a character
sequence seq of n characters starting at s, each widened using out.widen() (30.5.5.3), where n is the
number that would be computed as if by:
(4.1)
traits::length(s) for the overload where the first argument is of type basic_ostream<charT,
traits>& and the second is of type const charT*, and also for the overload where the first
argument is of type basic_ostream<char, traits>& and the second is of type const char*,
(4.2)
char_traits<char>::length(s) for the overload where the first argument is of type basic_-
ostream<charT, traits>& and the second is of type const char*,
(4.3)
traits::length(reinterpret_cast<const char*>(s)) for the other two overloads.
Determines padding for seq as described in 30.7.5.2.1. Inserts seq into out. Calls width(0).
5
Returns: out.
30.7.5.3
Unformatted output functions
[ostream.unformatted]
1
Each unformatted output function begins execution by constructing an object of class sentry. If this object
returns true, while converting to a value of type bool, the function endeavors to generate the requested
output. If an exception is thrown during output, then ios::badbit is turned on326 in *this’s error state.
If
(exceptions() & badbit) != 0 then the exception is rethrown. In any case, the unformatted output
function ends by destroying the sentry object, then, if no exception was thrown, returning the value specified
for the unformatted output function.
basic_ostream<charT, traits>& put(char_type c);
2
Effects: Behaves as an unformatted output function (as described above). After constructing a sentry
object, inserts the character c, if possible.327
3
Otherwise, calls setstate(badbit) (which may throw ios_base::failure (30.5.5.4)).
4
Returns: *this.
basic_ostream& write(const char_type* s, streamsize n);
5
Effects: Behaves as an unformatted output function (as described above). After constructing a sentry
object, obtains characters to insert from successive locations of an array whose first element is designated
by s.328 Characters are inserted until either of the following occurs:
(5.1)
n characters are inserted;
326) without causing an ios::failure to be thrown.
327) Note that this function is not overloaded on types signed char and unsigned char.
328) Note that this function is not overloaded on types signed char and unsigned char.
§ 30.7.5.3
1077
(5.2)
inserting in the output sequence fails (in which case the function calls setstate(badbit), which
may throw ios_base::failure (30.5.5.4)).
6
Returns: *this.
basic_ostream& flush();
7
Effects: Behaves as an unformatted output function (as described above). If rdbuf() is not a null
pointer, constructs a sentry object. If this object returns true when converted to a value of type bool
the function calls rdbuf()->pubsync(). If that function returns -1 calls setstate(badbit) (which
may throw ios_base::failure (30.5.5.4)). Otherwise, if the sentry object returns false, does nothing.
8
Returns: *this.
30.7.5.4
Standard basic_ostream manipulators
[ostream.manip]
template<class charT, class traits>
basic_ostream<charT, traits>& endl(basic_ostream<charT, traits>& os);
1
Effects: Calls os.put(os.widen(’\n’)), then os.flush().
2
Returns: os.
template<class charT, class traits>
basic_ostream<charT, traits>& ends(basic_ostream<charT, traits>& os);
3
Effects: Inserts a null character into the output sequence: calls os.put(charT()).
4
Returns: os.
template<class charT, class traits>
basic_ostream<charT, traits>& flush(basic_ostream<charT, traits>& os);
5
Effects: Calls os.flush().
6
Returns: os.
30.7.5.5
Rvalue stream insertion
[ostream.rvalue]
template<class charT, class traits, class T>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&& os, const T& x);
1
Effects: As if by: os << x;
2
Returns: os.
3
Remarks: This function shall not participate in overload resolution unless the expression os << x is
well-formed.
30.7.6
Standard manipulators
[std.manip]
1
The header <iomanip> defines several functions that support extractors and inserters that alter information
maintained by class ios_base and its derived classes.
unspecified resetiosflags(ios_base::fmtflags mask);
2
Returns: An object of unspecified type such that if out is an object of type basic_ostream<charT,
traits> then the expression out << resetiosflags(mask) behaves as if it called f(out, mask), or if
in is an object of type basic_istream<charT, traits> then the expression in >> resetiosflags(
mask) behaves as if it called f(in, mask), where the function f is defined as:329
void f(ios_base& str, ios_base::fmtflags mask) {
// reset specified flags
str.setf(ios_base::fmtflags(0), mask);
}
The expression out << resetiosflags(mask) shall have type basic_ostream<charT, traits>& and
value out. The expression in >> resetiosflags(mask) shall have type basic_istream<charT,
traits>& and value in.
329) The expression cin >> resetiosflags(ios_base::skipws) clears ios_base::skipws in the format flags stored in the
basic_istream<charT, traits> object cin (the same as cin >> noskipws), and the expression cout << resetiosflags(ios_-
base::showbase) clears ios_base::showbase in the format flags stored in the basic_ostream<charT, traits> object cout (the
same as cout << noshowbase).
§ 30.7.6
1078
unspecified setiosflags(ios_base::fmtflags mask);
3
Returns: An object of unspecified type such that if out is an object of type basic_ostream<charT,
traits> then the expression out << setiosflags(mask) behaves as if it called f(out, mask), or if in
is an object of type basic_istream<charT, traits> then the expression in >> setiosflags(mask)
behaves as if it called f(in, mask), where the function f is defined as:
void f(ios_base& str, ios_base::fmtflags mask) {
// set specified flags
str.setf(mask);
}
The expression out << setiosflags(mask) shall have type basic_ostream<charT, traits>& and
value out. The expression in >> setiosflags(mask) shall have type basic_istream<charT,
traits>& and value in.
unspecified setbase(int base);
4
Returns: An object of unspecified type such that if out is an object of type basic_ostream<charT,
traits> then the expression out << setbase(base) behaves as if it called f(out, base), or if in is
an object of type basic_istream<charT, traits> then the expression in >> setbase(base) behaves
as if it called f(in, base), where the function f is defined as:
void f(ios_base& str, int base) {
// set basefield
str.setf(base ==
8 ? ios_base::oct :
base == 10 ? ios_base::dec :
base == 16 ? ios_base::hex :
ios_base::fmtflags(0), ios_base::basefield);
}
The expression out << setbase(base) shall have type basic_ostream<charT, traits>& and value
out. The expression in >> setbase(base) shall have type basic_istream<charT, traits>& and
value in.
unspecified setfill(char_type c);
5
Returns: An object of unspecified type such that if out is an object of type basic_ostream<charT,
traits> and c has type charT then the expression out << setfill(c) behaves as if it called f(out,
c), where the function f is defined as:
template<class charT, class traits>
void f(basic_ios<charT, traits>& str, charT c) {
// set fill character
str.fill(c);
}
The expression out << setfill(c) shall have type basic_ostream<charT, traits>& and value out.
unspecified setprecision(int n);
6
Returns: An object of unspecified type such that if out is an object of type basic_ostream<charT,
traits> then the expression out << setprecision(n) behaves as if it called f(out, n), or if in is an
object of type basic_istream<charT, traits> then the expression in >> setprecision(n) behaves
as if it called f(in, n), where the function f is defined as:
void f(ios_base& str, int n) {
// set precision
str.precision(n);
}
The expression out << setprecision(n) shall have type basic_ostream<charT, traits>& and value
out. The expression in >> setprecision(n) shall have type basic_istream<charT, traits>& and
value in.
unspecified setw(int n);
7
Returns: An object of unspecified type such that if out is an instance of basic_ostream<charT,
traits> then the expression out << setw(n) behaves as if it called f(out, n), or if in is an object
§ 30.7.6
1079
of type basic_istream<charT, traits> then the expression in >> setw(n) behaves as if it called
f(in, n), where the function f is defined as:
void f(ios_base& str, int n) {
// set width
str.width(n);
}
The expression out << setw(n) shall have type basic_ostream<charT, traits>& and value out.
The expression in >> setw(n) shall have type basic_istream<charT, traits>& and value in.
30.7.7
Extended manipulators
[ext.manip]
1
The header <iomanip> defines several functions that support extractors and inserters that allow for the
parsing and formatting of sequences and values for money and time.
template<class moneyT> unspecified get_money(moneyT& mon, bool intl = false);
2
Requires: The type moneyT shall be either long double or a specialization of the basic_string
template (Clause 24).
3
Effects: The expression in >> get_money(mon, intl) described below behaves as a formatted input
function (30.7.4.2.1).
4
Returns: An object of unspecified type such that if in is an object of type basic_istream<charT,
traits> then the expression in >> get_money(mon, intl) behaves as if it called f(in, mon, intl),
where the function f is defined as:
template<class charT, class traits, class moneyT>
void f(basic_ios<charT, traits>& str, moneyT& mon, bool intl) {
using Iter
= istreambuf_iterator<charT, traits>;
using MoneyGet = money_get<charT, Iter>;
ios_base::iostate err = ios_base::goodbit;
const MoneyGet& mg = use_facet<MoneyGet>(str.getloc());
mg.get(Iter(str.rdbuf()), Iter(), intl, str, err, mon);
if (ios_base::goodbit != err)
str.setstate(err);
}
The expression in >> get_money(mon, intl) shall have type basic_istream<charT, traits>& and
value in.
template<class moneyT> unspecified put_money(const moneyT& mon, bool intl = false);
5
Requires: The type moneyT shall be either long double or a specialization of the basic_string
template (Clause 24).
6
Returns: An object of unspecified type such that if out is an object of type basic_ostream<charT,
traits> then the expression out << put_money(mon, intl) behaves as a formatted output function
(30.7.5.2.1) that calls f(out, mon, intl), where the function f is defined as:
template<class charT, class traits, class moneyT>
void f(basic_ios<charT, traits>& str, const moneyT& mon, bool intl) {
using Iter
= ostreambuf_iterator<charT, traits>;
using MoneyPut = money_put<charT, Iter>;
const MoneyPut& mp = use_facet<MoneyPut>(str.getloc());
const Iter end = mp.put(Iter(str.rdbuf()), intl, str, str.fill(), mon);
if (end.failed())
str.setstate(ios::badbit);
}
The expression out << put_money(mon, intl) shall have type basic_ostream<charT, traits>&
and value out.
§ 30.7.7
1080
template<class charT> unspecified get_time(struct tm* tmb, const charT* fmt);
7
Requires: The argument tmb shall be a valid pointer to an object of type struct tm. The argument fmt
shall be a valid pointer to an array of objects of type charT with char_traits<charT>::length(fmt)
elements.
8
Returns: An object of unspecified type such that if in is an object of type basic_istream<charT,
traits> then the expression in >> get_time(tmb, fmt) behaves as if it called f(in, tmb, fmt),
where the function f is defined as:
template<class charT, class traits>
void f(basic_ios<charT, traits>& str, struct tm* tmb, const charT* fmt) {
using Iter
= istreambuf_iterator<charT, traits>;
using TimeGet = time_get<charT, Iter>;
ios_base::iostate err = ios_base::goodbit;
const TimeGet& tg = use_facet<TimeGet>(str.getloc());
tg.get(Iter(str.rdbuf()), Iter(), str, err, tmb,
fmt, fmt + traits::length(fmt));
if (err != ios_base::goodbit)
str.setstate(err);
}
The expression in >> get_time(tmb, fmt) shall have type basic_istream<charT, traits>& and
value in.
template<class charT> unspecified put_time(const struct tm* tmb, const charT* fmt);
9
Requires: The argument tmb shall be a valid pointer to an object of type struct tm, and the argument
fmt shall be a valid pointer to an array of objects of type charT with char_traits<charT>::length(
fmt) elements.
10
Returns: An object of unspecified type such that if out is an object of type basic_ostream<charT,
traits> then the expression out << put_time(tmb, fmt) behaves as if it called f(out, tmb, fmt),
where the function f is defined as:
template<class charT, class traits>
void f(basic_ios<charT, traits>& str, const struct tm* tmb, const charT* fmt) {
using Iter
= ostreambuf_iterator<charT, traits>;
using TimePut = time_put<charT, Iter>;
const TimePut& tp = use_facet<TimePut>(str.getloc());
const Iter end = tp.put(Iter(str.rdbuf()), str, str.fill(), tmb,
fmt, fmt + traits::length(fmt));
if (end.failed())
str.setstate(ios_base::badbit);
}
The expression out << put_time(tmb, fmt) shall have type basic_ostream<charT, traits>& and
value out.
30.7.8
Quoted manipulators
[quoted.manip]
1
[Note: Quoted manipulators provide string insertion and extraction of quoted strings (for example, XML
and CSV formats). Quoted manipulators are useful in ensuring that the content of a string with embedded
spaces remains unchanged if inserted and then extracted via stream I/O. — end note ]
template<class charT>
unspecified quoted(const charT* s, charT delim = charT(’"’), charT escape = charT(’\\’));
template<class charT, class traits, class Allocator>
unspecified quoted(const basic_string<charT, traits, Allocator>& s,
charT delim = charT(’"’), charT escape = charT(’\\’));
§ 30.7.8
1081
template<class charT, class traits>
unspecified quoted(basic_string_view<charT, traits> s,
charT delim = charT(’"’), charT escape = charT(’\\’));
2
Returns: An object of unspecified type such that if out is an instance of basic_ostream with member
type char_type the same as charT and with member type traits_type, which in the second and third
forms is the same as traits, then the expression out << quoted(s, delim, escape) behaves as a
formatted output function (30.7.5.2.1) of out. This forms a character sequence seq, initially consisting
of the following elements:
(2.1)
delim.
(2.2)
Each character in s. If the character to be output is equal to escape or delim, as determined by
traits_type::eq, first output escape.
(2.3)
delim.
Let x be the number of elements initially in seq. Then padding is determined for seq as described
in 30.7.5.2.1, seq is inserted as if by calling out.rdbuf()->sputn(seq, n), where n is the larger of
out.width() and x, and out.width(0) is called. The expression out << quoted(s, delim, escape)
shall have type basic_ostream<charT, traits>& and value out.
template<class charT, class traits, class Allocator>
unspecified quoted(basic_string<charT, traits, Allocator>& s,
charT delim = charT(’"’), charT escape = charT(’\\’));
3
Returns: An object of unspecified type such that:
(3.1)
If in is an instance of basic_istream with member types char_type and traits_type the
same as charT and traits, respectively, then the expression in >> quoted(s, delim, escape)
behaves as if it extracts the following characters from in using operator>>(basic_istream<charT,
traits>&, charT&) (30.7.4.2.3) which may throw ios_base::failure (30.5.3.1.1):
(3.1.1)
If the first character extracted is equal to delim, as determined by traits_type::eq, then:
(3.1.1.1)
Turn off the skipws flag.
(3.1.1.2)
s.clear()
(3.1.1.3)
Until an unescaped delim character is reached or !in, extract characters from in and
append them to s, except that if an escape is reached, ignore it and append the next
character to s.
(3.1.1.4)
Discard the final delim character.
(3.1.1.5)
Restore the skipws flag to its original value.
(3.1.2)
Otherwise, in >> s.
(3.2)
If out is an instance of basic_ostream with member types char_type and traits_type the
same as charT and traits, respectively, then the expression out << quoted(s, delim, escape)
behaves as specified for the const basic_string<charT, traits, Allocator>& overload of the
quoted function.
The expression in >> quoted(s, delim, escape) shall have type basic_istream<charT, traits>&
and value in. The expression out << quoted(s, delim, escape) shall have type basic_ostream
<charT, traits>& and value out.
30.8
String-based streams
[string.streams]
30.8.1
Header <sstream> synopsis
[sstream.syn]
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT>>
class basic_stringbuf;
using stringbuf
= basic_stringbuf<char>;
using wstringbuf = basic_stringbuf<wchar_t>;
§ 30.8.1
1082
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT>>
class basic_istringstream;
using istringstream
= basic_istringstream<char>;
using wistringstream = basic_istringstream<wchar_t>;
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT>>
class basic_ostringstream;
using ostringstream
= basic_ostringstream<char>;
using wostringstream = basic_ostringstream<wchar_t>;
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT>>
class basic_stringstream;
using stringstream
= basic_stringstream<char>;
using wstringstream = basic_stringstream<wchar_t>;
}
1
The header <sstream> defines four class templates and eight types that associate stream buffers with objects
of class basic_string, as described in 24.3.
30.8.2
Class template basic_stringbuf
[stringbuf]
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT>>
class basic_stringbuf : public basic_streambuf<charT, traits>
{
public:
using char_type
= charT;
using int_type
= typename traits::int_type;
using pos_type
= typename traits::pos_type;
using off_type
= typename traits::off_type;
using traits_type
= traits;
using allocator_type = Allocator;
// 30.8.2.1, constructors
explicit basic_stringbuf(
ios_base::openmode which = ios_base::in | ios_base::out);
explicit basic_stringbuf(
const basic_string<charT, traits, Allocator>& str,
ios_base::openmode which = ios_base::in | ios_base::out);
basic_stringbuf(const basic_stringbuf& rhs) = delete;
basic_stringbuf(basic_stringbuf&& rhs);
// 30.8.2.2, assign and swap
basic_stringbuf& operator=(const basic_stringbuf& rhs) = delete;
basic_stringbuf& operator=(basic_stringbuf&& rhs);
void swap(basic_stringbuf& rhs);
// 30.8.2.3, get and set
basic_string<charT, traits, Allocator> str() const;
void str(const basic_string<charT, traits, Allocator>& s);
protected:
// 30.8.2.4, overridden virtual functions
int_type underflow() override;
int_type pbackfail(int_type c = traits::eof()) override;
int_type overflow (int_type c = traits::eof()) override;
basic_streambuf<charT, traits>* setbuf(charT*, streamsize) override;
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out) override;
§
30.8.2
1083
pos_type seekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out) override;
private:
ios_base::openmode mode;
// exposition only
};
template<class charT, class traits, class Allocator>
void swap(basic_stringbuf<charT, traits, Allocator>& x,
basic_stringbuf<charT, traits, Allocator>& y);
}
1
The class basic_stringbuf is derived from basic_streambuf to associate possibly the input sequence and
possibly the output sequence with a sequence of arbitrary characters. The sequence can be initialized from,
or made available as, an object of class basic_string.
2
For the sake of exposition, the maintained data is presented here as:
(2.1)
ios_base::openmode mode, has in set if the input sequence can be read, and out set if the output
sequence can be written.
30.8.2.1
basic_stringbuf constructors
[stringbuf.cons]
explicit basic_stringbuf(
ios_base::openmode which = ios_base::in | ios_base::out);
1
Effects: Constructs an object of class basic_stringbuf, initializing the base class with basic_-
streambuf() (30.6.3.1), and initializing mode with which.
2
Postconditions: str() == "".
explicit basic_stringbuf(
const basic_string<charT, traits, Allocator>& s,
ios_base::openmode which = ios_base::in | ios_base::out);
3
Effects: Constructs an object of class basic_stringbuf, initializing the base class with basic_-
streambuf() (30.6.3.1), and initializing mode with which. Then calls str(s).
basic_stringbuf(basic_stringbuf&& rhs);
4
Effects: Move constructs from the rvalue rhs. It is implementation-defined whether the sequence
pointers in *this (eback(), gptr(), egptr(), pbase(), pptr(), epptr()) obtain the values which
rhs had. Whether they do or not, *this and rhs reference separate buffers (if any at all) after the
construction. The openmode, locale and any other state of rhs is also copied.
5
Postconditions: Let rhs_p refer to the state of rhs just prior to this construction and let rhs_a refer
to the state of rhs just after this construction.
(5.1)
str() == rhs_p.str()
(5.2)
gptr() - eback() == rhs_p.gptr() - rhs_p.eback()
(5.3)
egptr() - eback() == rhs_p.egptr() - rhs_p.eback()
(5.4)
pptr() - pbase() == rhs_p.pptr() - rhs_p.pbase()
(5.5)
epptr() - pbase() == rhs_p.epptr() - rhs_p.pbase()
(5.6)
if (eback()) eback() != rhs_a.eback()
(5.7)
if (gptr()) gptr() != rhs_a.gptr()
(5.8)
if (egptr()) egptr() != rhs_a.egptr()
(5.9)
if (pbase()) pbase() != rhs_a.pbase()
(5.10)
if (pptr()) pptr() != rhs_a.pptr()
(5.11)
if (epptr()) epptr() != rhs_a.epptr()
§ 30.8.2.1
1084
30.8.2.2
Assign and swap
[stringbuf.assign]
basic_stringbuf& operator=(basic_stringbuf&& rhs);
1
Effects: After the move assignment *this has the observable state it would have had if it had been
move constructed from rhs (see 30.8.2.1).
2
Returns: *this.
void swap(basic_stringbuf& rhs);
3
Effects: Exchanges the state of *this and rhs.
template<class charT, class traits, class Allocator>
void swap(basic_stringbuf<charT, traits, Allocator>& x,
basic_stringbuf<charT, traits, Allocator>& y);
4
Effects: As if by x.swap(y).
30.8.2.3
Member functions
[stringbuf.members]
basic_string<charT, traits, Allocator> str() const;
1
Returns: A basic_string object whose content is equal to the basic_stringbuf underlying character
sequence. If the basic_stringbuf was created only in input mode, the resultant basic_string
contains the character sequence in the range [eback(), egptr()). If the basic_stringbuf was
created with which & ios_base::out being nonzero then the resultant basic_string contains the
character sequence in the range [pbase(), high_mark), where high_mark represents the position one
past the highest initialized character in the buffer. Characters can be initialized by writing to the stream,
by constructing the basic_stringbuf with a basic_string, or by calling the str(basic_string)
member function. In the case of calling the str(basic_string) member function, all characters
initialized prior to the call are now considered uninitialized (except for those characters re-initialized
by the new basic_string). Otherwise the basic_stringbuf has been created in neither input nor
output mode and a zero length basic_string is returned.
void str(const basic_string<charT, traits, Allocator>& s);
2
Effects: Copies the content of s into the basic_stringbuf underlying character sequence and initializes
the input and output sequences according to mode.
3
Postconditions: If mode & ios_base::out is nonzero, pbase() points to the first underlying character
and epptr() >= pbase() + s.size() holds; in addition, if mode & ios_base::ate is nonzero, pptr()
== pbase() + s.size() holds, otherwise pptr() == pbase() is true. If mode & ios_base::in is
nonzero, eback() points to the first underlying character, and both gptr() == eback() and egptr()
== eback() + s.size() hold.
30.8.2.4
Overridden virtual functions
[stringbuf.virtuals]
int_type underflow() override;
1
Returns: If the input sequence has a read position available, returns traits::to_int_type(*gptr()).
Otherwise, returns traits::eof(). Any character in the underlying buffer which has been initialized
is considered to be part of the input sequence.
int_type pbackfail(int_type c = traits::eof()) override;
2
Effects: Puts back the character designated by c to the input sequence, if possible, in one of three ways:
(2.1)
If traits::eq_int_type(c, traits::eof()) returns false and if the input sequence has a
putback position available, and if traits::eq(to_char_type(c), gptr()[-1]) returns true,
assigns gptr() - 1 to gptr().
Returns: c.
(2.2)
If traits::eq_int_type(c, traits::eof()) returns false and if the input sequence has a
putback position available, and if mode & ios_base::out is nonzero, assigns c to *--gptr().
Returns: c.
(2.3)
If traits::eq_int_type(c, traits::eof()) returns true and if the input sequence has a
putback position available, assigns gptr() - 1 to gptr().
§ 30.8.2.4
1085
Returns: traits::not_eof(c).
3
Returns: As specified above, or traits::eof() to indicate failure.
4
Remarks: If the function can succeed in more than one of these ways, it is unspecified which way is
chosen.
int_type overflow(int_type c = traits::eof()) override;
5
Effects: Appends the character designated by c to the output sequence, if possible, in one of two ways:
(5.1)
If traits::eq_int_type(c, traits::eof()) returns false and if either the output sequence
has a write position available or the function makes a write position available (as described below),
the function calls sputc(c).
Signals success by returning c.
(5.2)
If traits::eq_int_type(c, traits::eof()) returns true, there is no character to append.
Signals success by returning a value other than traits::eof().
6
Remarks: The function can alter the number of write positions available as a result of any call.
7
Returns: As specified above, or traits::eof() to indicate failure.
8
The function can make a write position available only if (mode & ios_base::out) != 0. To make a
write position available, the function reallocates (or initially allocates) an array object with a sufficient
number of elements to hold the current array object (if any), plus at least one additional write position.
If (mode & ios_base::in) != 0, the function alters the read end pointer egptr() to point just past
the new write position.
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out) override;
9
Effects: Alters the stream position within one of the controlled sequences, if possible, as indicated in
Table 115.
Table 115 — seekoff positioning
Conditions
Result
(which & ios_base::in) == ios_-
positions the input sequence
base::in
(which & ios_base::out) == ios_-
positions the output sequence
base::out
(which & (ios_base::in |
positions both the input and the output sequences
ios_base::out)) ==
(ios_base::in |
ios_base::out)
and way == either
ios_base::beg or
ios_base::end
Otherwise
the positioning operation fails.
10
For a sequence to be positioned, the function determines newoff as indicated in Table 116. If the
sequence’s next pointer (either gptr() or pptr()) is a null pointer and newoff is nonzero, the positioning
operation fails.
11
If
(newoff + off) < 0, or if newoff + off refers to an uninitialized character (30.8.2.3), the posi-
tioning operation fails. Otherwise, the function assigns xbeg + newoff + off to the next pointer
xnext.
12
Returns: pos_type(newoff), constructed from the resultant offset newoff (of type off_type), that
stores the resultant stream position, if possible. If the positioning operation fails, or if the constructed
object cannot represent the resultant stream position, the return value is pos_type(off_type(-1)).
§ 30.8.2.4
1086
Table 116 — newoff values
Condition
newoff Value
way == ios_base::beg
0
way == ios_base::cur
the next pointer minus the begin-
ning pointer (xnext - xbeg).
way == ios_base::end the high mark pointer minus the
beginning pointer (high_mark -
xbeg).
pos_type seekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out) override;
13
Effects: Equivalent to seekoff(off_type(sp), ios_base::beg, which).
14
Returns: sp to indicate success, or pos_type(off_type(-1)) to indicate failure.
basic_streambuf<charT, traits>* setbuf(charT* s, streamsize n);
15
Effects: implementation-defined, except that setbuf(0, 0) has no effect.
16
Returns: this.
30.8.3
Class template basic_istringstream
[istringstream]
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT>>
class basic_istringstream : public basic_istream<charT, traits> {
public:
using char_type
= charT;
using int_type
= typename traits::int_type;
using pos_type
= typename traits::pos_type;
using off_type
= typename traits::off_type;
using traits_type
= traits;
using allocator_type = Allocator;
// 30.8.3.1, constructors
explicit basic_istringstream(
ios_base::openmode which = ios_base::in);
explicit basic_istringstream(
const basic_string<charT, traits, Allocator>& str,
ios_base::openmode which = ios_base::in);
basic_istringstream(const basic_istringstream& rhs) = delete;
basic_istringstream(basic_istringstream&& rhs);
// 30.8.3.2, assign and swap
basic_istringstream& operator=(const basic_istringstream& rhs)
=
delete;
basic_istringstream& operator=(basic_istringstream&& rhs);
void swap(basic_istringstream& rhs);
// 30.8.3.3, members
basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
basic_string<charT, traits, Allocator> str() const;
void str(const basic_string<charT, traits, Allocator>& s);
private:
basic_stringbuf<charT, traits, Allocator> sb; // exposition only
};
template<class charT, class traits, class Allocator>
void swap(basic_istringstream<charT, traits, Allocator>& x,
basic_istringstream<charT, traits, Allocator>& y);
}
§
30.8.3
1087
1
The class basic_istringstream<charT, traits, Allocator> supports reading objects of class basic_-
string<charT, traits, Allocator>. It uses a basic_stringbuf<charT, traits, Allocator> object to
control the associated storage. For the sake of exposition, the maintained data is presented here as:
(1.1)
sb, the stringbuf object.
30.8.3.1
basic_istringstream constructors
[istringstream.cons]
explicit basic_istringstream(ios_base::openmode which = ios_base::in);
1
Effects: Constructs an object of class basic_istringstream<charT, traits>, initializing the base
class with basic_istream<charT, traits>(&sb) (30.7.4.1) and initializing sb with basic_string-
buf<charT, traits, Allocator>(which | ios_base::in)) (30.8.2.1).
explicit basic_istringstream(
const basic_string<charT, traits, Allocator>& str,
ios_base::openmode which = ios_base::in);
2
Effects: Constructs an object of class basic_istringstream<charT, traits>, initializing the base
class with basic_istream<charT, traits>(&sb) (30.7.4.1) and initializing sb with basic_string-
buf<charT, traits, Allocator>(str, which | ios_base::in)) (30.8.2.1).
basic_istringstream(basic_istringstream&& rhs);
3
Effects: Move constructs from the rvalue rhs. This is accomplished by move constructing the base
class, and the contained basic_stringbuf. Next basic_istream<charT, traits>::set_rdbuf(&sb)
is called to install the contained basic_stringbuf.
30.8.3.2
Assign and swap
[istringstream.assign]
basic_istringstream& operator=(basic_istringstream&& rhs);
1
Effects: Move assigns the base and members of *this from the base and corresponding members of
rhs.
2
Returns: *this.
void swap(basic_istringstream& rhs);
3
Effects: Exchanges the state of *this and rhs by calling basic_istream<charT, traits>::swap(rhs)
and sb.swap(rhs.sb).
template<class charT, class traits, class Allocator>
void swap(basic_istringstream<charT, traits, Allocator>& x,
basic_istringstream<charT, traits, Allocator>& y);
4
Effects: As if by x.swap(y).
30.8.3.3
Member functions
[istringstream.members]
basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
1
Returns: const_cast<basic_stringbuf<charT, traits, Allocator>*>(&sb).
basic_string<charT, traits, Allocator> str() const;
2
Returns: rdbuf()->str().
void str(const basic_string<charT, traits, Allocator>& s);
3
Effects: Calls rdbuf()->str(s).
30.8.4
Class template basic_ostringstream
[ostringstream]
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT>>
class basic_ostringstream : public basic_ostream<charT, traits> {
public:
using char_type
= charT;
using int_type
= typename traits::int_type;
using pos_type
= typename traits::pos_type;
§ 30.8.4
1088
using off_type
= typename traits::off_type;
using traits_type
= traits;
using allocator_type = Allocator;
// 30.8.4.1, constructors
explicit basic_ostringstream(
ios_base::openmode which = ios_base::out);
explicit basic_ostringstream(
const basic_string<charT, traits, Allocator>& str,
ios_base::openmode which = ios_base::out);
basic_ostringstream(const basic_ostringstream& rhs) = delete;
basic_ostringstream(basic_ostringstream&& rhs);
// 30.8.4.2, assign and swap
basic_ostringstream& operator=(const basic_ostringstream& rhs)
=
delete;
basic_ostringstream& operator=(basic_ostringstream&& rhs);
void swap(basic_ostringstream& rhs);
// 30.8.4.3, members
basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
basic_string<charT, traits, Allocator> str() const;
void str(const basic_string<charT, traits, Allocator>& s);
private:
basic_stringbuf<charT, traits, Allocator> sb; // exposition only
};
template<class charT, class traits, class Allocator>
void swap(basic_ostringstream<charT, traits, Allocator>& x,
basic_ostringstream<charT, traits, Allocator>& y);
}
1
The class basic_ostringstream<charT, traits, Allocator> supports writing objects of class basic_-
string<charT, traits, Allocator>. It uses a basic_stringbuf object to control the associated storage.
For the sake of exposition, the maintained data is presented here as:
(1.1)
sb, the stringbuf object.
30.8.4.1
basic_ostringstream constructors
[ostringstream.cons]
explicit basic_ostringstream(
ios_base::openmode which = ios_base::out);
1
Effects: Constructs an object of class basic_ostringstream<charT, traits>, initializing the base
class with basic_ostream<charT, traits>(&sb) (30.7.5.1) and initializing sb with basic_string-
buf<charT, traits, Allocator>(which | ios_base::out)) (30.8.2.1).
explicit basic_ostringstream(
const basic_string<charT, traits, Allocator>& str,
ios_base::openmode which = ios_base::out);
2
Effects: Constructs an object of class basic_ostringstream<charT, traits>, initializing the base
class with basic_ostream<charT, traits>(&sb) (30.7.5.1) and initializing sb with basic_string-
buf<charT, traits, Allocator>(str, which | ios_base::out)) (30.8.2.1).
basic_ostringstream(basic_ostringstream&& rhs);
3
Effects: Move constructs from the rvalue rhs. This is accomplished by move constructing the base
class, and the contained basic_stringbuf. Next basic_ostream<charT, traits>::set_rdbuf(&sb)
is called to install the contained basic_stringbuf.
30.8.4.2
Assign and swap
[ostringstream.assign]
basic_ostringstream& operator=(basic_ostringstream&& rhs);
1
Effects: Move assigns the base and members of *this from the base and corresponding members of
rhs.
§ 30.8.4.2
1089
2
Returns: *this.
void swap(basic_ostringstream& rhs);
3
Effects: Exchanges the state of *this and rhs by calling basic_ostream<charT, traits>::swap(rhs)
and sb.swap(rhs.sb).
template<class charT, class traits, class Allocator>
void swap(basic_ostringstream<charT, traits, Allocator>& x,
basic_ostringstream<charT, traits, Allocator>& y);
4
Effects: As if by x.swap(y).
30.8.4.3
Member functions
[ostringstream.members]
basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
1
Returns: const_cast<basic_stringbuf<charT, traits, Allocator>*>(&sb).
basic_string<charT, traits, Allocator> str() const;
2
Returns: rdbuf()->str().
void str(const basic_string<charT, traits, Allocator>& s);
3
Effects: Calls rdbuf()->str(s).
30.8.5
Class template basic_stringstream
[stringstream]
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT>>
class basic_stringstream : public basic_iostream<charT, traits> {
public:
using char_type
= charT;
using int_type
= typename traits::int_type;
using pos_type
= typename traits::pos_type;
using off_type
= typename traits::off_type;
using traits_type
= traits;
using allocator_type = Allocator;
// 30.8.5.1, constructors
explicit basic_stringstream(
ios_base::openmode which = ios_base::out | ios_base::in);
explicit basic_stringstream(
const basic_string<charT, traits, Allocator>& str,
ios_base::openmode which = ios_base::out | ios_base::in);
basic_stringstream(const basic_stringstream& rhs) = delete;
basic_stringstream(basic_stringstream&& rhs);
// 30.8.5.2, assign and swap
basic_stringstream& operator=(const basic_stringstream& rhs)
=
delete;
basic_stringstream& operator=(basic_stringstream&& rhs);
void swap(basic_stringstream& rhs);
// 30.8.5.3, members
basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
basic_string<charT, traits, Allocator> str() const;
void str(const basic_string<charT, traits, Allocator>& str);
private:
basic_stringbuf<charT, traits> sb;
// exposition only
};
template<class charT, class traits, class Allocator>
void swap(basic_stringstream<charT, traits, Allocator>& x,
basic_stringstream<charT, traits, Allocator>& y);
}
§
30.8.5
1090
1
The class template basic_stringstream<charT, traits> supports reading and writing from objects of
class basic_string<charT, traits, Allocator>. It uses a basic_stringbuf<charT, traits, Alloca-
tor> object to control the associated sequence. For the sake of exposition, the maintained data is presented
here as
(1.1)
sb, the stringbuf object.
30.8.5.1
basic_stringstream constructors
[stringstream.cons]
explicit basic_stringstream(
ios_base::openmode which = ios_base::out | ios_base::in);
1
Effects: Constructs an object of class basic_stringstream<charT, traits>, initializing the base
class with basic_iostream<charT, traits>(&sb) (30.7.4.6.1) and initializing sb with basic_string-
buf<charT, traits, Allocator>(which).
explicit basic_stringstream(
const basic_string<charT, traits, Allocator>& str,
ios_base::openmode which = ios_base::out | ios_base::in);
2
Effects: Constructs an object of class basic_stringstream<charT, traits>, initializing the base
class with basic_iostream<charT, traits>(&sb) (30.7.4.6.1) and initializing sb with basic_string-
buf<charT, traits, Allocator>(str, which).
basic_stringstream(basic_stringstream&& rhs);
3
Effects: Move constructs from the rvalue rhs. This is accomplished by move constructing the base
class, and the contained basic_stringbuf. Next basic_istream<charT, traits>::set_rdbuf(&sb)
is called to install the contained basic_stringbuf.
30.8.5.2
Assign and swap
[stringstream.assign]
basic_stringstream& operator=(basic_stringstream&& rhs);
1
Effects: Move assigns the base and members of *this from the base and corresponding members of
rhs.
2
Returns: *this.
void swap(basic_stringstream& rhs);
3
Effects: Exchanges the state of *this and rhs by calling basic_iostream<charT,traits>::swap(rhs)
and sb.swap(rhs.sb).
template<class charT, class traits, class Allocator>
void swap(basic_stringstream<charT, traits, Allocator>& x,
basic_stringstream<charT, traits, Allocator>& y);
4
Effects: As if by x.swap(y).
30.8.5.3
Member functions
[stringstream.members]
basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
1
Returns: const_cast<basic_stringbuf<charT, traits, Allocator>*>(&sb)
basic_string<charT, traits, Allocator> str() const;
2
Returns: rdbuf()->str().
void str(const basic_string<charT, traits, Allocator>& str);
3
Effects: Calls rdbuf()->str(str).
30.9
File-based streams
[file.streams]
30.9.1
Header <fstream> synopsis
[fstream.syn]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_filebuf;
using filebuf
= basic_filebuf<char>;
using wfilebuf = basic_filebuf<wchar_t>;
§ 30.9.1
1091
template<class charT, class traits = char_traits<charT>>
class basic_ifstream;
using ifstream
= basic_ifstream<char>;
using wifstream = basic_ifstream<wchar_t>;
template<class charT, class traits = char_traits<charT>>
class basic_ofstream;
using ofstream
= basic_ofstream<char>;
using wofstream = basic_ofstream<wchar_t>;
template<class charT, class traits = char_traits<charT>>
class basic_fstream;
using fstream
= basic_fstream<char>;
using wfstream = basic_fstream<wchar_t>;
}
1
The header <fstream> defines four class templates and eight types that associate stream buffers with files
and assist reading and writing files.
2
[ Note: The class template basic_filebuf treats a file as a source or sink of bytes. In an environment that
uses a large character set, the file typically holds multibyte character sequences and the basic_filebuf
object converts those multibyte sequences into wide character sequences.
— end note ]
3
In this subclause, member functions taking arguments of const filesystem::path::value_type* are
only be provided on systems where filesystem::path::value_type (30.11.7) is not char. [Note: These
functions enable class path support for systems with a wide native path character type, such as wchar_t.
— end note ]
30.9.2
Class template basic_filebuf
[filebuf]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_filebuf : public basic_streambuf<charT, traits> {
public:
using char_type
= charT;
using int_type
= typename traits::int_type;
using pos_type
= typename traits::pos_type;
using off_type
= typename traits::off_type;
using traits_type = traits;
// 30.9.2.1, constructors/destructor
basic_filebuf();
basic_filebuf(const basic_filebuf& rhs) = delete;
basic_filebuf(basic_filebuf&& rhs);
virtual ~basic_filebuf();
// 30.9.2.2, assign and swap
basic_filebuf& operator=(const basic_filebuf& rhs) = delete;
basic_filebuf& operator=(basic_filebuf&& rhs);
void swap(basic_filebuf& rhs);
// 30.9.2.3, members
bool is_open() const;
basic_filebuf* open(const char* s, ios_base::openmode mode);
basic_filebuf* open(const filesystem::path::value_type* s,
ios_base::openmode mode);
// wide systems only; see
30.9.1
basic_filebuf* open(const string& s,
ios_base::openmode mode);
basic_filebuf* open(const filesystem::path& s,
ios_base::openmode mode);
basic_filebuf* close();
protected:
// 30.9.2.4, overridden virtual functions
streamsize showmanyc() override;
§
30.9.2
1092
int_type underflow() override;
int_type uflow() override;
int_type pbackfail(int_type c = traits::eof()) override;
int_type overflow (int_type c = traits::eof()) override;
basic_streambuf<charT, traits>* setbuf(char_type* s,
streamsize n) override;
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out) override;
pos_type seekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out) override;
int
sync() override;
void
imbue(const locale& loc) override;
};
template<class charT, class traits>
void swap(basic_filebuf<charT, traits>& x,
basic_filebuf<charT, traits>& y);
}
1
The class basic_filebuf<charT, traits> associates both the input sequence and the output sequence with
a file.
2
The restrictions on reading and writing a sequence controlled by an object of class basic_filebuf<charT,
traits> are the same as for reading and writing with the C standard library FILEs.
3
In particular:
(3.1)
If the file is not open for reading the input sequence cannot be read.
(3.2)
If the file is not open for writing the output sequence cannot be written.
(3.3)
A joint file position is maintained for both the input sequence and the output sequence.
4
An instance of basic_filebuf behaves as described in 30.9.2 provided traits::pos_type is fpos<traits::
state_type>. Otherwise the behavior is undefined.
5
In order to support file I/O and multibyte/wide character conversion, conversions are performed using
members of a facet, referred to as a_codecvt in following subclauses, obtained as if by
const codecvt<charT, char, typename traits::state_type>& a_codecvt =
use_facet<codecvt<charT, char, typename traits::state_type>>(getloc());
30.9.2.1
basic_filebuf constructors
[filebuf.cons]
basic_filebuf();
1
Effects: Constructs an object of class basic_filebuf<charT, traits>, initializing the base class with
basic_streambuf<charT, traits>() (30.6.3.1).
2
Postconditions: is_open() == false.
basic_filebuf(basic_filebuf&& rhs);
3
Effects: Move constructs from the rvalue rhs. It is implementation-defined whether the sequence
pointers in *this (eback(), gptr(), egptr(), pbase(), pptr(), epptr()) obtain the values which
rhs had. Whether they do or not, *this and rhs reference separate buffers (if any at all) after the
construction. Additionally *this references the file which rhs did before the construction, and rhs
references no file after the construction. The openmode, locale and any other state of rhs is also copied.
4
Postconditions: Let rhs_p refer to the state of rhs just prior to this construction and let rhs_a refer
to the state of rhs just after this construction.
(4.1)
is_open() == rhs_p.is_open()
(4.2)
rhs_a.is_open() == false
(4.3)
gptr() - eback() == rhs_p.gptr() - rhs_p.eback()
(4.4)
egptr() - eback() == rhs_p.egptr() - rhs_p.eback()
§ 30.9.2.1
1093
(4.5)
pptr() - pbase() == rhs_p.pptr() - rhs_p.pbase()
(4.6)
epptr() - pbase() == rhs_p.epptr() - rhs_p.pbase()
(4.7)
if (eback()) eback() != rhs_a.eback()
(4.8)
if (gptr()) gptr() != rhs_a.gptr()
(4.9)
if (egptr()) egptr() != rhs_a.egptr()
(4.10)
if (pbase()) pbase() != rhs_a.pbase()
(4.11)
if (pptr()) pptr() != rhs_a.pptr()
(4.12)
if (epptr()) epptr() != rhs_a.epptr()
virtual ~basic_filebuf();
5
Effects: Destroys an object of class basic_filebuf<charT, traits>. Calls close(). If an exception
occurs during the destruction of the object, including the call to close(), the exception is caught but
not rethrown (see 20.5.5.12).
30.9.2.2
Assign and swap
[filebuf.assign]
basic_filebuf& operator=(basic_filebuf&& rhs);
1
Effects: Calls close() then move assigns from rhs. After the move assignment *this has the observable
state it would have had if it had been move constructed from rhs (see 30.9.2.1).
2
Returns: *this.
void swap(basic_filebuf& rhs);
3
Effects: Exchanges the state of *this and rhs.
template<class charT, class traits>
void swap(basic_filebuf<charT, traits>& x,
basic_filebuf<charT, traits>& y);
4
Effects: As if by x.swap(y).
30.9.2.3
Member functions
[filebuf.members]
bool is_open() const;
1
Returns: true if a previous call to open succeeded (returned a non-null value) and there has been no
intervening call to close.
basic_filebuf* open(const char* s, ios_base::openmode mode);
basic_filebuf* open(const filesystem::path::value_type* s,
ios_base::openmode mode);
// wide systems only; see 30.9.1
2
Effects: If is_open() != false, returns a null pointer. Otherwise, initializes the filebuf as required.
It then opens a file, if possible, whose name is the ntbs s (as if by calling fopen(s, modstr)). The
ntbs modstr is determined from mode & ~ios_base::ate as indicated in Table 117. If mode is not
some combination of flags shown in the table then the open fails.
3
If the open operation succeeds and (mode & ios_base::ate) != 0, positions the file to the end (as if
by calling fseek(file, 0, SEEK_END)).330
4
If the repositioning operation fails, calls close() and returns a null pointer to indicate failure.
5
Returns: this if successful, a null pointer otherwise.
basic_filebuf* open(const string& s, ios_base::openmode mode);
basic_filebuf* open(const filesystem::path& s, ios_base::openmode mode);
6
Returns: open(s.c_str(), mode);
330) The macro SEEK_END is defined, and the function signatures fopen(const char*, const char*) and fseek(FILE*, long,
int) are declared, in <cstdio> (30.12.1).
§ 30.9.2.3
1094
Table 117 — File open modes
ios_base flag combination
stdio equivalent
binary in out trunc app
+
"w"
+
+ "a"
+ "a"
+
+
"w"
+
"r"
+
+
"r+"
+
+
+
"w+"
+
+
+ "a+"
+
+ "a+"
+
+
"wb"
+
+
+ "ab"
+
+ "ab"
+
+
+
"wb"
+
+
"rb"
+
+
+
"r+b"
+
+
+
+
"w+b"
+
+
+
+ "a+b"
+
+
+ "a+b"
basic_filebuf* close();
7
Effects: If is_open() == false, returns a null pointer. If a put area exists, calls overflow(traits::
eof()) to flush characters. If the last virtual member function called on *this (between underflow,
overflow, seekoff, and seekpos) was overflow then calls a_codecvt.unshift (possibly several
times) to determine a termination sequence, inserts those characters and calls overflow(traits::
eof()) again. Finally, regardless of whether any of the preceding calls fails or throws an exception,
the function closes the file (as if by calling fclose(file)). If any of the calls made by the function,
including fclose, fails, close fails by returning a null pointer. If one of these calls throws an exception,
the exception is caught and rethrown after closing the file.
8
Returns: this on success, a null pointer otherwise.
9
Postconditions: is_open() == false.
30.9.2.4
Overridden virtual functions
[filebuf.virtuals]
streamsize showmanyc() override;
1
Effects: Behaves the same as basic_streambuf::showmanyc() (30.6.3.4).
2
Remarks: An implementation might well provide an overriding definition for this function signature if
it can determine that more characters can be read from the input sequence.
int_type underflow() override;
3
Effects: Behaves according to the description of basic_streambuf<charT, traits>::underflow(),
with the specialization that a sequence of characters is read from the input sequence as if by reading
from the associated file into an internal buffer (extern_buf) and then as if by doing:
char extern_buf[XSIZE];
char* extern_end;
charT intern_buf[ISIZE];
charT* intern_end;
codecvt_base::result r =
a_codecvt.in(state, extern_buf, extern_buf+XSIZE, extern_end,
intern_buf, intern_buf+ISIZE, intern_end);
§ 30.9.2.4
1095
This shall be done in such a way that the class can recover the position (fpos_t) corresponding to each
character between intern_buf and intern_end. If the value of r indicates that a_codecvt.in() ran
out of space in intern_buf, retry with a larger intern_buf.
int_type uflow() override;
4
Effects: Behaves according to the description of basic_streambuf<charT, traits>::uflow(), with
the specialization that a sequence of characters is read from the input with the same method as used
by underflow.
int_type pbackfail(int_type c = traits::eof()) override;
5
Effects: Puts back the character designated by c to the input sequence, if possible, in one of three ways:
(5.1)
If traits::eq_int_type(c, traits::eof()) returns false and if the function makes a putback
position available and if traits::eq(to_char_type(c), gptr()[-1]) returns true, decrements
the next pointer for the input sequence, gptr().
Returns: c.
(5.2)
If traits::eq_int_type(c, traits::eof()) returns false and if the function makes a putback
position available and if the function is permitted to assign to the putback position, decrements
the next pointer for the input sequence, and stores c there.
Returns: c.
(5.3)
If traits::eq_int_type(c, traits::eof()) returns true, and if either the input sequence has
a putback position available or the function makes a putback position available, decrements the
next pointer for the input sequence, gptr().
Returns: traits::not_eof(c).
6
Returns: As specified above, or traits::eof() to indicate failure.
7
Remarks: If is_open() == false, the function always fails.
8
The function does not put back a character directly to the input sequence.
9
If the function can succeed in more than one of these ways, it is unspecified which way is chosen. The
function can alter the number of putback positions available as a result of any call.
int_type overflow(int_type c = traits::eof()) override;
10
Effects: Behaves according to the description of basic_streambuf<charT, traits>::overflow(c),
except that the behavior of “consuming characters” is performed by first converting as if by:
charT* b = pbase();
charT* p = pptr();
charT* end;
char xbuf[XSIZE];
char* xbuf_end;
codecvt_base::result r =
a_codecvt.out(state, b, p, end, xbuf, xbuf+XSIZE, xbuf_end);
and then
(10.1)
If r
== codecvt_base::error then fail.
(10.2)
If r
== codecvt_base::noconv then output characters from b up to (and not including) p.
(10.3)
If r
== codecvt_base::partial then output to the file characters from xbuf up to xbuf_end,
and repeat using characters from end to p. If output fails, fail (without repeating).
(10.4)
Otherwise output from xbuf to xbuf_end, and fail if output fails. At this point if b != p and b
== end (xbuf isn’t large enough) then increase XSIZE and repeat from the beginning.
11
Returns: traits::not_eof(c) to indicate success, and traits::eof() to indicate failure. If is_-
open() == false, the function always fails.
basic_streambuf* setbuf(char_type* s, streamsize n) override;
12
Effects: If setbuf(0, 0) is called on a stream before any I/O has occurred on that stream, the stream
becomes unbuffered. Otherwise the results are implementation-defined.
“Unbuffered” means that
pbase() and pptr() always return null and output to the file should appear as soon as possible.
§ 30.9.2.4
1096
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out) override;
13
Effects: Let width denote a_codecvt.encoding(). If is_open() == false, or off != 0 && width
<= 0, then the positioning operation fails. Otherwise, if way != basic_ios::cur or off != 0, and
if the last operation was output, then update the output sequence and write any unshift sequence.
Next, seek to the new position: if width > 0, call fseek(file, width * off, whence), otherwise
call fseek(file, 0, whence).
14
Remarks: “The last operation was output” means either the last virtual operation was overflow or
the put buffer is non-empty.
“Write any unshift sequence” means, if width if less than zero then
call a_codecvt.unshift(state, xbuf, xbuf+XSIZE, xbuf_end) and output the resulting unshift
sequence. The function determines one of three values for the argument whence, of type int, as
indicated in Table 118.
Table 118 — seekoff effects
way Value
stdio Equivalent
basic_ios::beg SEEK_SET
basic_ios::cur SEEK_CUR
basic_ios::end SEEK_END
15
Returns: A newly constructed pos_type object that stores the resultant stream position, if possible. If
the positioning operation fails, or if the object cannot represent the resultant stream position, returns
pos_type(off_type(-1)).
pos_type seekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out) override;
16
Alters the file position, if possible, to correspond to the position stored in sp (as described below).
Altering the file position performs as follows:
1. if (om & ios_base::out) != 0, then update the output sequence and write any unshift sequence;
2. set the file position to sp as if by a call to fsetpos;
3. if (om & ios_base::in) != 0, then update the input sequence;
where om is the open mode passed to the last call to open(). The operation fails if is_open() returns
false.
17
If sp is an invalid stream position, or if the function positions neither sequence, the positioning operation
fails. If sp has not been obtained by a previous successful call to one of the positioning functions
(seekoff or seekpos) on the same file the effects are undefined.
18
Returns: sp on success. Otherwise returns pos_type(off_type(-1)).
int sync() override;
19
Effects: If a put area exists, calls filebuf::overflow to write the characters to the file, then flushes
the file as if by calling fflush(file). If a get area exists, the effect is implementation-defined.
void imbue(const locale& loc) override;
20
Requires: If the file is not positioned at its beginning and the encoding of the current locale as
determined by a_codecvt.encoding() is state-dependent (25.4.1.4.2) then that facet is the same as
the corresponding facet of loc.
21
Effects: Causes characters inserted or extracted after this call to be converted according to loc until
another call of imbue.
22
Remarks: This may require reconversion of previously converted characters. This in turn may require
the implementation to be able to reconstruct the original contents of the file.
§ 30.9.2.4
1097
30.9.3
Class template basic_ifstream
[ifstream]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_ifstream : public basic_istream<charT, traits> {
public:
using char_type
= charT;
using int_type
= typename traits::int_type;
using pos_type
= typename traits::pos_type;
using off_type
= typename traits::off_type;
using traits_type = traits;
// 30.9.3.1, constructors
basic_ifstream();
explicit basic_ifstream(const char* s,
ios_base::openmode mode = ios_base::in);
explicit basic_ifstream(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::in);
// wide systems only; see
30.9.1
explicit basic_ifstream(const string& s,
ios_base::openmode mode = ios_base::in);
explicit basic_ifstream(const filesystem::path& s,
ios_base::openmode mode = ios_base::in);
basic_ifstream(const basic_ifstream& rhs) = delete;
basic_ifstream(basic_ifstream&& rhs);
// 30.9.3.2, assign and swap
basic_ifstream& operator=(const basic_ifstream& rhs) = delete;
basic_ifstream& operator=(basic_ifstream&& rhs);
void swap(basic_ifstream& rhs);
// 30.9.3.3, members
basic_filebuf<charT, traits>* rdbuf() const;
bool is_open() const;
void open(const char* s, ios_base::openmode mode = ios_base::in);
void open(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::in);
// wide systems only; see 30.9.1
void open(const string& s, ios_base::openmode mode = ios_base::in);
void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in);
void close();
private:
basic_filebuf<charT, traits> sb; // exposition only
};
template<class charT, class traits>
void swap(basic_ifstream<charT, traits>& x,
basic_ifstream<charT, traits>& y);
}
1
The class basic_ifstream<charT, traits> supports reading from named files. It uses a basic_filebuf<
charT, traits> object to control the associated sequence. For the sake of exposition, the maintained data
is presented here as:
(1.1)
sb, the filebuf object.
30.9.3.1
basic_ifstream constructors
[ifstream.cons]
basic_ifstream();
1
Effects: Constructs an object of class basic_ifstream<charT, traits>, initializing the base class with
basic_istream<charT, traits>(&sb) (30.7.4.1.1) and initializing sb with basic_filebuf<charT,
traits>()) (30.9.2.1).
explicit basic_ifstream(const char* s,
ios_base::openmode mode = ios_base::in);
§ 30.9.3.1
1098
explicit basic_ifstream(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::in);
// wide systems only; see 30.9.1
2
Effects: Constructs an object of class basic_ifstream<charT, traits>, initializing the base class with
basic_istream<charT, traits>(&sb) (30.7.4.1.1) and initializing sb with basic_filebuf<charT,
traits>()) (30.9.2.1), then calls rdbuf()->open(s, mode | ios_base::in). If that function returns
a null pointer, calls setstate(failbit).
explicit basic_ifstream(const string& s,
ios_base::openmode mode = ios_base::in);
explicit basic_ifstream(const filesystem::path& s,
ios_base::openmode mode = ios_base::in);
3
Effects: The same as basic_ifstream(s.c_str(), mode).
basic_ifstream(basic_ifstream&& rhs);
4
Effects: Move constructs from the rvalue rhs. This is accomplished by move constructing the base
class, and the contained basic_filebuf. Next basic_istream<charT, traits>::set_rdbuf(&sb)
is called to install the contained basic_filebuf.
30.9.3.2
Assign and swap
[ifstream.assign]
basic_ifstream& operator=(basic_ifstream&& rhs);
1
Effects: Move assigns the base and members of *this from the base and corresponding members of
rhs.
2
Returns: *this.
void swap(basic_ifstream& rhs);
3
Effects: Exchanges the state of *this and rhs by calling basic_istream<charT, traits>::swap(rhs)
and sb.swap(rhs.sb).
template<class charT, class traits>
void swap(basic_ifstream<charT, traits>& x,
basic_ifstream<charT, traits>& y);
4
Effects: As if by x.swap(y).
30.9.3.3
Member functions
[ifstream.members]
basic_filebuf<charT, traits>* rdbuf() const;
1
Returns: const_cast<basic_filebuf<charT, traits>*>(&sb).
bool is_open() const;
2
Returns: rdbuf()->is_open().
void open(const char* s, ios_base::openmode mode = ios_base::in);
void open(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::in);
// wide systems only; see 30.9.1
3
Effects: Calls rdbuf()->open(s, mode | ios_base::in). If that function does not return a null
pointer calls clear(), otherwise calls setstate(failbit) (which may throw ios_base::failure)
(30.5.5.4).
void open(const string& s, ios_base::openmode mode = ios_base::in);
void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in);
4
Effects: Calls open(s.c_str(), mode).
void close();
5
Effects: Calls rdbuf()->close() and, if that function returns a null pointer, calls setstate(failbit)
(which may throw ios_base::failure) (30.5.5.4).
§ 30.9.3.3
1099
30.9.4
Class template basic_ofstream
[ofstream]
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_ofstream : 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.9.4.1, constructors
basic_ofstream();
explicit basic_ofstream(const char* s,
ios_base::openmode mode = ios_base::out);
explicit basic_ofstream(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::out);
// wide systems only; see 30.9.1
explicit basic_ofstream(const string& s,
ios_base::openmode mode = ios_base::out);
explicit basic_ofstream(const filesystem::path& s,
ios_base::openmode mode = ios_base::out);
basic_ofstream(const basic_ofstream& rhs) = delete;
basic_ofstream(basic_ofstream&& rhs);
// 30.9.4.2, assign and swap
basic_ofstream& operator=(const basic_ofstream& rhs) = delete;
basic_ofstream& operator=(basic_ofstream&& rhs);
void swap(basic_ofstream& rhs);
// 30.9.4.3, members
basic_filebuf<charT, traits>* rdbuf() const;
bool is_open() const;
void open(const char* s, ios_base::openmode mode = ios_base::out);
void open(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::out);
// wide systems only; see 30.9.1
void open(const string& s, ios_base::openmode mode = ios_base::out);
void open(const filesystem::path& s, ios_base::openmode mode = ios_base::out);
void close();
private:
basic_filebuf<charT, traits> sb; // exposition only
};
template<class charT, class traits>
void swap(basic_ofstream<charT, traits>& x,
basic_ofstream<charT, traits>& y);
}
1
The class basic_ofstream<charT, traits> supports writing to named files. It uses a basic_filebuf<
charT, traits> object to control the associated sequence. For the sake of exposition, the maintained data
is presented here as:
(1.1)
sb, the filebuf object.
30.9.4.1
basic_ofstream constructors
[ofstream.cons]
basic_ofstream();
1
Effects: Constructs an object of class basic_ofstream<charT, traits>, initializing the base class with
basic_ostream<charT, traits>(&sb) (30.7.5.1.1) and initializing sb with basic_filebuf<charT,
traits>()) (30.9.2.1).
explicit basic_ofstream(const char* s,
ios_base::openmode mode = ios_base::out);
§ 30.9.4.1
1100
explicit basic_ofstream(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::out); // wide systems only; see 30.9.1
2
Effects: Constructs an object of class basic_ofstream<charT, traits>, initializing the base class with
basic_ostream<charT, traits>(&sb) (30.7.5.1.1) and initializing sb with basic_filebuf<charT,
traits>()) (30.9.2.1), then calls rdbuf()->open(s, mode | ios_base::out). If that function re-
turns a null pointer, calls setstate(failbit).
explicit basic_ofstream(const string& s,
ios_base::openmode mode = ios_base::out);
explicit basic_ofstream(const filesystem::path& s,
ios_base::openmode mode = ios_base::out);
3
Effects: The same as basic_ofstream(s.c_str(), mode).
basic_ofstream(basic_ofstream&& rhs);
4
Effects: Move constructs from the rvalue rhs. This is accomplished by move constructing the base
class, and the contained basic_filebuf. Next basic_ostream<charT, traits>::set_rdbuf(&sb)
is called to install the contained basic_filebuf.
30.9.4.2
Assign and swap
[ofstream.assign]
basic_ofstream& operator=(basic_ofstream&& rhs);
1
Effects: Move assigns the base and members of *this from the base and corresponding members of
rhs.
2
Returns: *this.
void swap(basic_ofstream& rhs);
3
Effects: Exchanges the state of *this and rhs by calling basic_ostream<charT, traits>::swap(rhs)
and sb.swap(rhs.sb).
template<class charT, class traits>
void swap(basic_ofstream<charT, traits>& x,
basic_ofstream<charT, traits>& y);
4
Effects: As if by x.swap(y).
30.9.4.3
Member functions
[ofstream.members]
basic_filebuf<charT, traits>* rdbuf() const;
1
Returns: const_cast<basic_filebuf<charT, traits>*>(&sb).
bool is_open() const;
2
Returns: rdbuf()->is_open().
void open(const char* s, ios_base::openmode mode = ios_base::out);
void open(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::out);
// wide systems only; see 30.9.1
3
Effects: Calls rdbuf()->open(s, mode | ios_base::out). If that function does not return a null
pointer calls clear(), otherwise calls setstate(failbit) (which may throw ios_base::failure)
(30.5.5.4).
void close();
4
Effects: Calls rdbuf()->close() and, if that function fails (returns a null pointer), calls setstate(
failbit) (which may throw ios_base::failure) (30.5.5.4).
void open(const string& s, ios_base::openmode mode = ios_base::out);
void open(const filesystem::path& s, ios_base::openmode mode = ios_base::out);
5
Effects: Calls open(s.c_str(), mode).
§ 30.9.4.3
1101

 

 

 

 

 

 

 

Content      ..     35      36      37      38     ..