Files
cppdraft_translate/cppdraft/istream.md
2025-10-25 03:02:53 +03:00

266 lines
12 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[istream]
# 31 Input/output library [[input.output]](./#input.output)
## 31.7 Formatting and manipulators [[iostream.format]](iostream.format#istream)
### 31.7.5 Input streams [[input.streams]](input.streams#istream)
#### 31.7.5.2 Class template basic_istream [istream]
#### [31.7.5.2.1](#general) General [[istream.general]](istream.general)
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4296)
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]](basic.fundamental "6.9.2Fundamental types"))
in lieu of *extended-floating-point-type*[.](#general-1.sentence-1)
[🔗](#lib:basic_istream)
namespace std {template<class charT, class traits = char_traits<charT>>class basic_istream : virtual public basic_ios<charT, traits> {public:// types (inherited from [basic_ios](ios "31.5.4Class template basic_­ios[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]](#cons "31.7.5.2.2Constructors"), constructor/destructorexplicit basic_istream(basic_streambuf<charT, traits>* sb); virtual ~basic_istream(); // [[istream.sentry]](#sentry "31.7.5.2.4Class basic_­istream::sentry"), prefix/suffixclass sentry; // [[istream.formatted]](istream.formatted "31.7.5.3Formatted input functions"), 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]](istream.unformatted "31.7.5.4Unformatted input functions"), 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]](#cons "31.7.5.2.2Constructors"), copy/move constructor basic_istream(const basic_istream&) = delete;
basic_istream(basic_istream&& rhs); // [[istream.assign]](#assign "31.7.5.2.3Assignment and swap"), assignment and swap basic_istream& operator=(const basic_istream&) = delete;
basic_istream& operator=(basic_istream&& rhs); void swap(basic_istream& rhs); }; // [[istream.extractors]](istream.extractors "31.7.5.3.3basic_­istream::operator>>"), character extraction templatestemplate<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, 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]);}
[2](#general-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4399)
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[.](#general-2.sentence-1)
[3](#general-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4406)
Two groups of member function signatures share common properties:
the[*formatted input functions*](#def:formatted_input_functions) (or[*extractors*](#def:extractors))
and the[*unformatted input functions.*](#def:unformatted_input_functions.) Both groups of input functions are described as if they obtain (or[*extract*](#def:extract))
input[*characters*](#def:characters) by callingrdbuf()->sbumpc() orrdbuf()->sgetc()[.](#general-3.sentence-1)
They may use other public members ofistream[.](#general-3.sentence-2)
#### [31.7.5.2.2](#cons) Constructors [[istream.cons]](istream.cons)
[🔗](#lib:basic_istream,constructor)
`explicit basic_istream(basic_streambuf<charT, traits>* sb);
`
[1](#cons-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4434)
*Effects*: Initializes the base class subobject withbasic_ios::init(sb) ([[basic.ios.cons]](basic.ios.cons "31.5.4.2Constructors"))[.](#cons-1.sentence-1)
[2](#cons-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4439)
*Postconditions*: gcount() == 0[.](#cons-2.sentence-1)
[🔗](#lib:basic_istream,constructor_)
`basic_istream(basic_istream&& rhs);
`
[3](#cons-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4451)
*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[.](#cons-3.sentence-1)
[🔗](#lib:basic_istream,destructor)
`virtual ~basic_istream();
`
[4](#cons-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4465)
*Remarks*: Does not perform any operations ofrdbuf()[.](#cons-4.sentence-1)
#### [31.7.5.2.3](#assign) Assignment and swap [[istream.assign]](istream.assign)
[🔗](#lib:operator=,basic_istream)
`basic_istream& operator=(basic_istream&& rhs);
`
[1](#assign-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4479)
*Effects*: Equivalent to swap(rhs)[.](#assign-1.sentence-1)
[2](#assign-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4483)
*Returns*: *this[.](#assign-2.sentence-1)
[🔗](#lib:swap,basic_istream)
`void swap(basic_istream& rhs);
`
[3](#assign-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4494)
*Effects*: Calls basic_ios<charT, traits>::swap(rhs)[.](#assign-3.sentence-1)
Exchanges the values returned by gcount() andrhs.gcount()[.](#assign-3.sentence-2)
#### [31.7.5.2.4](#sentry) Class basic_istream::sentry [[istream.sentry]](istream.sentry)
[🔗](#lib:basic_istream::sentry)
namespace std {template<class charT, class traits>class basic_istream<charT, traits>::sentry {bool *ok_*; // *exposition only*public:explicit sentry(basic_istream& is, bool noskipws = false); ~sentry(); explicit operator bool() const { return *ok_*; } sentry(const sentry&) = delete;
sentry& operator=(const sentry&) = delete; };}
[1](#sentry-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4522)
The classsentry defines a class that is responsible for doing exception safe prefix and suffix
operations[.](#sentry-1.sentence-1)
[🔗](#lib:sentry,constructor)
`explicit sentry(basic_istream& is, bool noskipws = false);
`
[2](#sentry-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4536)
*Effects*: Ifis.good() isfalse,
calls is.setstate(failbit)[.](#sentry-2.sentence-1)
Otherwise,
prepares for formatted or
unformatted input[.](#sentry-2.sentence-2)
First, ifis.tie() is not a null pointer, the
function callsis.tie()->flush() to synchronize the output sequence with any associated external
C stream[.](#sentry-2.sentence-3)
Except that this call can be suppressed if the put area ofis.tie() is empty[.](#sentry-2.sentence-4)
Further an implementation is allowed to defer the call toflush until a
call ofis.rdbuf()->underflow() occurs[.](#sentry-2.sentence-5)
If no such call occurs before thesentry object is destroyed, the call toflush may be eliminated entirely[.](#sentry-2.sentence-6)[272](#footnote-272 "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.")
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[.](#sentry-2.sentence-7)
Ifis.rdbuf()->sbumpc() oris.rdbuf()->sgetc() returnstraits::eof(),
the function callssetstate(failbit | eofbit) (which may throwios_base::failure)[.](#sentry-2.sentence-8)
[3](#sentry-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4587)
*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[.](#sentry-3.sentence-1)
[4](#sentry-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4597)
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.
[5](#sentry-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4606)
If, after any preparation is completed,is.good() istrue,*ok_* != false otherwise,*ok_* == false[.](#sentry-5.sentence-1)
During preparation, the constructor may callsetstate(failbit) (which may throwios_base::failure ([[iostate.flags]](iostate.flags "31.5.4.4Flags functions")))[.](#sentry-5.sentence-2)[273](#footnote-273 "The sentry constructor and destructor can also perform additional implementation-dependent operations.")
[🔗](#lib:sentry,destructor)
`~sentry();
`
[6](#sentry-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4635)
*Effects*: None[.](#sentry-6.sentence-1)
[🔗](#lib:operator_bool,basic_istream::sentry)
`explicit operator bool() const;
`
[7](#sentry-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4646)
*Returns*: *ok_*[.](#sentry-7.sentence-1)
[272)](#footnote-272)[272)](#footnoteref-272)
This will be possible only in functions
that are part of the library[.](#footnote-272.sentence-1)
The semantics of the constructor used in user code is as specified[.](#footnote-272.sentence-2)
[273)](#footnote-273)[273)](#footnoteref-273)
Thesentry constructor and destructor
can also perform additionalimplementation-dependent operations[.](#footnote-273.sentence-1)