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

4.4 KiB
Raw Blame History

[istream.sentry]

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.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; };}

1

#

The classsentry defines a class that is responsible for doing exception safe prefix and suffix operations.

🔗

explicit sentry(basic_istream& is, bool noskipws = false);

2

#

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).

3

#

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.

4

#

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.

5

#

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();

6

#

Effects: None.

🔗

explicit operator bool() const;

7

#

Returns: ok_.

272)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.

273)273)

Thesentry constructor and destructor can also perform additionalimplementation-dependent operations.