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.
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): 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; // [istream.cons], constructor/destructor explicit basic_istream(basic_streambuf<charT, traits>* sb); virtual ~basic_istream(); // [istream::sentry], prefix/suffix class sentry; // [istream.formatted], 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); // [istream.unformatted], 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); 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: // [istream.cons], copy/move constructor basic_istream(const basic_istream& rhs) = delete; basic_istream(basic_istream&& rhs); // [istream.assign], assign and swap basic_istream& operator=(const basic_istream& rhs) = delete; basic_istream& operator=(basic_istream&& rhs); void swap(basic_istream& rhs); }; // [istream.extractors], 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*); }
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.
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.
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 ([iostate.flags]), before returning.
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.
explicit basic_istream(basic_streambuf<charT, traits>* sb);
Effects: Constructs an object of class basic_istream, initializing the base class subobject with basic_ios::init(sb) ([basic.ios.cons]).
basic_istream(basic_istream&& rhs);
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();
basic_istream& operator=(basic_istream&& rhs);
void swap(basic_istream& rhs);
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;
};
}
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);
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.305 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).
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.
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.
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 ([iostate.flags]))306
~sentry();
explicit operator bool() const;
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.
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 on307 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.
This is done without causing an ios::failure to be thrown.
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);
As in the case of the inserters, these extractors depend on the locale's num_get<> object to perform parsing the input stream data. These extractors behave as formatted input functions (as described in [istream.formatted.reqmts]). 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);
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; val = numeric_limits<short>::max(); } else val = static_cast<short>(lval); setstate(err);
operator>>(int& val);
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);
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& (*pf)(basic_istream<charT, traits>&));
Effects: None. This extractor does not behave as a formatted input function (as described in [istream.formatted.reqmts]).
basic_istream<charT, traits>&
operator>>(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
Effects: Calls pf(*this). This extractor does not behave as a formatted input function (as described in [istream.formatted.reqmts]).
basic_istream<charT, traits>& operator>>(ios_base& (*pf)(ios_base&));
Effects: Calls pf(*this).309 This extractor does not behave as a formatted input function (as described in [istream.formatted.reqmts]).
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);
Effects: Behaves like a formatted input member (as described in [istream.formatted.reqmts]) 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.
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).
If the function extracted no characters, it calls setstate(failbit), which may throw ios_base::failure ([iostate.flags]).
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);
Effects: Behaves like a formatted input member (as described in [istream.formatted.reqmts]) 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).
basic_istream<charT, traits>& operator>>(basic_streambuf<charT, traits>* sb);
Effects: Behaves as an unformatted input function. If sb is null, calls setstate(failbit), which may throw ios_base::failure ([iostate.flags]). 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:
end-of-file occurs on the input sequence;
inserting in the output sequence fails (in which case the character to be inserted is not extracted);
an exception occurs (in which case the exception is caught).
If the function inserts no characters, it calls setstate(failbit), which may throw ios_base::failure ([iostate.flags]). If it inserted no characters because it caught an exception thrown while extracting characters from *this and failbit is on in exceptions() ([iostate.flags]), then the caught exception is rethrown.
See, for example, the function signature ws(basic_istream&).
See, for example, the function signature dec(ios_base&).
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 on310 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;
Effects: None. This member function does not behave as an unformatted input function (as described above).
Returns: The number of characters extracted by the last unformatted input member function called for the object.
int_type get();
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 ([iostate.flags]),
basic_istream<charT, traits>& get(char_type& c);
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.311 Otherwise, the function calls setstate(failbit) (which may throw ios_base::failure ([iostate.flags])).
basic_istream<charT, traits>& get(char_type* s, streamsize n, char_type delim);
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.312 Characters are extracted and stored until any of the following occurs:
n is less than one or n - 1 characters are stored;
end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit));
traits::eq(c, delim) for the next available input character c (in which case c is not extracted).
If the function stores no characters, it calls setstate(failbit) (which may throw ios_base::failure ([iostate.flags])). In any case, if n is greater than zero it then stores a null character into the next successive location of the array.
basic_istream<charT, traits>& get(char_type* s, streamsize n);
basic_istream<charT, traits>& get(basic_streambuf<char_type, traits>& sb, char_type delim);
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:
end-of-file occurs on the input sequence;
inserting in the output sequence fails (in which case the character to be inserted is not extracted);
traits::eq(c, delim) for the next available input character c (in which case c is not extracted);
an exception occurs (in which case, the exception is caught but not rethrown).
If the function inserts no characters, it calls setstate(failbit), which may throw ios_base::failure ([iostate.flags]).
basic_istream<charT, traits>& get(basic_streambuf<char_type, traits>& sb);
basic_istream<charT, traits>& getline(char_type* s, streamsize n, char_type delim);
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.313 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));
2.traits::eq(c, delim) for the next available input character c (in which case the input character is extracted but not stored);314
3.n is less than one or n - 1 characters are stored (in which case the function calls setstate(failbit)).
If the function extracts no characters, it calls setstate(failbit) (which may throw ios_base::failure ([iostate.flags])).316
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.
[ 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);
basic_istream<charT, traits>& ignore(streamsize n = 1, int_type delim = traits::eof());
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:
n != numeric_limits<streamsize>::max() and n characters have been extracted so far
end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit), which may throw ios_base::failure ([iostate.flags]));
traits::eq_int_type(traits::to_int_type(c), delim) for the next available input character c (in which case c is extracted).
int_type peek();
Effects: Behaves as an unformatted input function (as described above). After constructing a sentry object, reads but does not extract the current input character.
basic_istream<charT, traits>& read(char_type* s, streamsize n);
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.317 Characters are extracted and stored until either of the following occurs:
n characters are stored;
end-of-file occurs on the input sequence (in which case the function calls setstate(failbit | eofbit), which may throw ios_base::failure ([iostate.flags])).
streamsize readsome(char_type* s, streamsize n);
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. If rdbuf()->in_avail() == -1, calls setstate(eofbit) (which may throw ios_base::failure ([iostate.flags])), and extracts no characters;
If rdbuf()->in_avail() == 0, extracts no characters
If rdbuf()->in_avail() > 0, extracts min(rdbuf()->in_avail(), n)).
basic_istream<charT, traits>& putback(char_type c);
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 ([iostate.flags])). [ Note: This function extracts no characters, so the value returned by the next call to gcount() is 0. — end note ]
basic_istream<charT, traits>& unget();
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 ([iostate.flags])). [ Note: This function extracts no characters, so the value returned by the next call to gcount() is 0. — end note ]
int sync();
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 ([iostate.flags]), and returns -1. Otherwise, returns zero.
pos_type tellg();
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().
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);
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).
basic_istream<charT, traits>& seekg(off_type off, ios_base::seekdir dir);
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).
This is done without causing an ios::failure to be thrown.
Note that this function is not overloaded on types signed char and unsigned char.
Note that this function is not overloaded on types signed char and unsigned char.
Note that this function is not overloaded on types signed char and unsigned char.
Since the final input character is “extracted”, it is counted in the gcount(), even though it is not stored.
This allows an input line which exactly fills the buffer, without setting failbit. This is different behavior than the historical AT&T implementation.
This implies an empty input line will not cause failbit to be set.
Note that this function is not overloaded on types signed char and unsigned char.
template <class charT, class traits>
basic_istream<charT, traits>& ws(basic_istream<charT, traits>& is);
Effects: Behaves as an unformatted input function, 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. If ws stops extracting characters because there are no more available it sets eofbit, but not failbit.
template <class charT, class traits, class T>
basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>&& is, T&& x);
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; // [iostream.cons], constructor explicit basic_iostream(basic_streambuf<charT, traits>* sb); // [iostream.dest], destructor virtual ~basic_iostream(); protected: // [iostream.cons], constructor basic_iostream(const basic_iostream& rhs) = delete; basic_iostream(basic_iostream&& rhs); // [iostream.assign], assign and swap basic_iostream& operator=(const basic_iostream& rhs) = delete; basic_iostream& operator=(basic_iostream&& rhs); void swap(basic_iostream& rhs); }; }
The class template basic_iostream inherits a number of functions that allow reading input and writing output to sequences controlled by a stream buffer.
explicit basic_iostream(basic_streambuf<charT, traits>* sb);
Effects: Constructs an object of class basic_iostream, initializing the base class subobjects with basic_istream<charT, traits>(sb) and basic_ostream<charT, traits>(sb).
basic_iostream(basic_iostream&& rhs);
virtual ~basic_iostream();
basic_iostream& operator=(basic_iostream&& rhs);
void swap(basic_iostream& rhs);