12 KiB
[istream]
31 Input/output library [input.output]
31.7 Formatting and manipulators [iostream.format]
31.7.5 Input streams [input.streams]
31.7.5.2 Class template basic_istream [istream]
31.7.5.2.1 General [istream.general]
When a function is specified with a type placeholder of extended-floating-point-type, the implementation provides overloads for all cv-unqualified extended floating-point types ([basic.fundamental]) in lieu of extended-floating-point-type.
namespace std {template<class charT, class traits = char_traits>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/destructorexplicit basic_istream(basic_streambuf<charT, traits>* sb); virtual ~basic_istream(); // [istream.sentry], prefix/suffixclass sentry; // [istream.formatted], formatted input basic_istream& operator>>(basic_istream& (*pf)(basic_istream&)); basic_istream& operator>>(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&)); basic_istream& operator>>(ios_base& (*pf)(ios_base&));
basic_istream& operator>>(bool& n); basic_istream& operator>>(short& n); basic_istream& operator>>(unsigned short& n); basic_istream& operator>>(int& n); basic_istream& operator>>(unsigned int& n); basic_istream& operator>>(long& n); basic_istream& operator>>(unsigned long& n); basic_istream& operator>>(long long& n); basic_istream& operator>>(unsigned long long& n); basic_istream& operator>>(float& f); basic_istream& operator>>(double& f); basic_istream& operator>>(long double& f); basic_istream& operator>>(extended-floating-point-type& f);
basic_istream& operator>>(void*& p); basic_istream& operator>>(basic_streambuf<char_type, traits>* sb); // [istream.unformatted], unformatted input streamsize gcount() const; int_type get(); basic_istream& get(char_type& c); basic_istream& get(char_type* s, streamsize n); basic_istream& get(char_type* s, streamsize n, char_type delim); basic_istream& get(basic_streambuf<char_type, traits>& sb); basic_istream& get(basic_streambuf<char_type, traits>& sb, char_type delim);
basic_istream& getline(char_type* s, streamsize n); basic_istream& getline(char_type* s, streamsize n, char_type delim);
basic_istream& ignore(streamsize n = 1, int_type delim = traits::eof()); basic_istream& ignore(streamsize n, char_type delim); int_type peek(); basic_istream& read (char_type* s, streamsize n); streamsize readsome(char_type* s, streamsize n);
basic_istream& putback(char_type c); basic_istream& unget(); int sync();
pos_type tellg(); basic_istream& seekg(pos_type); basic_istream& seekg(off_type, ios_base::seekdir); protected:// [istream.cons], copy/move constructor basic_istream(const basic_istream&) = delete; basic_istream(basic_istream&& rhs); // [istream.assign], assignment and swap basic_istream& operator=(const basic_istream&) = delete; basic_istream& operator=(basic_istream&& rhs); void swap(basic_istream& rhs); }; // [istream.extractors], character extraction templatestemplate<class charT, class traits> basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>&, charT&); template basic_istream<char, traits>& operator>>(basic_istream<char, traits>&, unsigned char&); template basic_istream<char, traits>& operator>>(basic_istream<char, traits>&, signed char&); template<class charT, class traits, size_t N> basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>&, charT(&)[N]); template<class traits, size_t N> basic_istream<char, traits>& operator>>(basic_istream<char, traits>&, unsigned char(&)[N]); template<class traits, size_t N> basic_istream<char, traits>& operator>>(basic_istream<char, traits>&, signed char(&)[N]);}
The class templatebasic_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: theformatted input functions (orextractors) and theunformatted input functions. Both groups of input functions are described as if they obtain (orextract) inputcharacters by callingrdbuf()->sbumpc() orrdbuf()->sgetc().
They may use other public members ofistream.
31.7.5.2.2 Constructors [istream.cons]
explicit basic_istream(basic_streambuf<charT, traits>* sb);
Effects: Initializes the base class subobject withbasic_ios::init(sb) ([basic.ios.cons]).
Postconditions: gcount() == 0.
basic_istream(basic_istream&& rhs);
Effects: Default constructs the base class, copies thegcount() from rhs, callsbasic_ios<charT, traits>::move(rhs) to initialize the base class, and sets the gcount() for rhs to 0.
virtual ~basic_istream();
Remarks: Does not perform any operations ofrdbuf().
31.7.5.2.3 Assignment and swap [istream.assign]
basic_istream& operator=(basic_istream&& rhs);
Effects: Equivalent to swap(rhs).
Returns: *this.
void swap(basic_istream& rhs);
Effects: Calls basic_ios<charT, traits>::swap(rhs).
Exchanges the values returned by gcount() andrhs.gcount().
31.7.5.2.4 Class basic_istream::sentry [istream.sentry]
namespace std {template<class charT, class traits>class basic_istream<charT, traits>::sentry {bool ok_; // exposition onlypublic:explicit sentry(basic_istream& is, bool noskipws = false); ~sentry(); explicit operator bool() const { return ok_; } sentry(const sentry&) = delete; sentry& operator=(const sentry&) = delete; };}
The classsentry defines a class that is responsible for doing exception safe prefix and suffix operations.
explicit sentry(basic_istream& is, bool noskipws = false);
Effects: Ifis.good() isfalse, calls is.setstate(failbit).
Otherwise, prepares for formatted or unformatted input.
First, ifis.tie() is not a null pointer, the function callsis.tie()->flush() to synchronize the output sequence with any associated external C stream.
Except that this call can be suppressed if the put area ofis.tie() is empty.
Further an implementation is allowed to defer the call toflush until a call ofis.rdbuf()->underflow() occurs.
If no such call occurs before thesentry object is destroyed, the call toflush may be eliminated entirely.272
If noskipws is zero andis.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.
Ifis.rdbuf()->sbumpc() oris.rdbuf()->sgetc() returnstraits::eof(), the function callssetstate(failbit | eofbit) (which may throwios_base::failure).
Remarks: The constructorexplicit sentry(basic_istream& 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& ctype = use_facet<ctype>(is.getloc());if (ctype.is(ctype.space, c) != 0)// c is a whitespace character.
If, after any preparation is completed,is.good() istrue,ok_ != false otherwise,ok_ == false.
During preparation, the constructor may callsetstate(failbit) (which may throwios_base::failure ([iostate.flags])).273
~sentry();
Effects: None.
explicit operator bool() const;
Returns: ok_.
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.
Thesentry constructor and destructor can also perform additionalimplementation-dependent operations.