Files
2025-10-25 03:02:53 +03:00

114 lines
4.4 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.sentry]
# 31 Input/output library [[input.output]](./#input.output)
## 31.7 Formatting and manipulators [[iostream.format]](iostream.format#istream.sentry)
### 31.7.5 Input streams [[input.streams]](input.streams#istream.sentry)
#### 31.7.5.2 Class template basic_istream [[istream]](istream#sentry)
#### 31.7.5.2.4 Class basic_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](#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[.](#1.sentence-1)
[🔗](#lib:sentry,constructor)
`explicit sentry(basic_istream& is, bool noskipws = false);
`
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4536)
*Effects*: Ifis.good() isfalse,
calls is.setstate(failbit)[.](#2.sentence-1)
Otherwise,
prepares for formatted or
unformatted input[.](#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[.](#2.sentence-3)
Except that this call can be suppressed if the put area ofis.tie() is empty[.](#2.sentence-4)
Further an implementation is allowed to defer the call toflush until a
call ofis.rdbuf()->underflow() occurs[.](#2.sentence-5)
If no such call occurs before thesentry object is destroyed, the call toflush may be eliminated entirely[.](#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[.](#2.sentence-7)
Ifis.rdbuf()->sbumpc() oris.rdbuf()->sgetc() returnstraits::eof(),
the function callssetstate(failbit | eofbit) (which may throwios_base::failure)[.](#2.sentence-8)
[3](#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[.](#3.sentence-1)
[4](#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](#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[.](#5.sentence-1)
During preparation, the constructor may callsetstate(failbit) (which may throwios_base::failure ([[iostate.flags]](iostate.flags "31.5.4.4Flags functions")))[.](#5.sentence-2)[273](#footnote-273 "The sentry constructor and destructor can also perform additional implementation-dependent operations.")
[🔗](#lib:sentry,destructor)
`~sentry();
`
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4635)
*Effects*: None[.](#6.sentence-1)
[🔗](#lib:operator_bool,basic_istream::sentry)
`explicit operator bool() const;
`
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L4646)
*Returns*: *ok_*[.](#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)