std::basic_istream::sentry(3)
πΈ NAME β std::basic_istream::sentry πΈ
Okay bestie, this is the std::basic_istream::sentry(3) page from the C++ Standard Libary manual, and it's literally the tiny main-character bodyguard for your input stream π
β¨.
std::basic_istream::sentry β the sentry class. No cap, that's the name.
π¦ Synopsis π¦
class sentry;
An object of class basic_istream::sentry is constructed in local scope at the beginning of each member function of std::basic_istream that performs input (both formatted and unformatted). Its constructor prepares the input stream: checks if the stream is already in a failed state, flushes the tie()'d output streams, skips leading whitespace unless noskipws flag is set, and performs other implementation-defined tasks if necessary. All cleanup, if necessary, is performed in the destructor, so that it is guaranteed to happen if exceptions are thrown during input.
π Member types π
traits_typeβ Traits.
π Member functions π
Lowkey tiny section, highkey important:
- constructor β constructs the sentry object. All the preparation tasks are done here (public member function).
- destructor β finalizes the stream object after formatted input or after exception, if necessary (public member function).
operator=β not copy assignable[deleted](public member function).operator boolβ checks if the preparation of the stream object was successful (public member function).
π std::basic_istream::sentry::sentry π
explicit sentry( std::basic_istream<CharT, Traits>& is, bool noskipws = false );
Prepares the stream for formatted input. Here's the whole prep routine, bestie β read it fr fr:
- If
is.good()is false, callsis.setstate(std::ios_base::failbit)and returns. - Otherwise, if
is.tie()is not a null pointer, callsis.tie()->flush()to synchronize the output sequence with external streams. This call can be suppressed if the put area ofis.tie()is empty. The implementation may defer the call toflush()until a call ofis.rdbuf()->underflow()occurs. If no such call occurs before the sentry object is destroyed, it may be eliminated entirely. - If
noskipwsis zero andis.flags() & std::ios_base::skipwsis nonzero, the function extracts and discards all whitespace characters until the next available character is not a whitespace character (as determined by the currently imbued locale inis). Ifis.rdbuf()->sbumpc()oris.rdbuf()->sgetc()returnstraits::eof(), the function callssetstate(std::ios_base::failbit | std::ios_base::eofbit)(which may throwstd::ios_base::failure). - Additional implementation-defined preparation may take place, which may call
setstate(std::ios_base::failbit)(which may throwstd::ios_base::failure). - If after preparation is completed,
is.good() == true, then any subsequent calls tooperator boolwill return true.
π Parameters π
isβ input stream to prepare.noskipwsβtrueif whitespace should not be skipped.
π₯ Exceptions π₯
std::ios_base::failure if the end of file condition occurs when skipping whitespace.
π¦ std::basic_istream::sentry::~sentry π¦
~sentry();
Does nothing. Literally nothing, it's off the clock πβ¨.
β std::basic_istream::sentry::operator bool β
explicit operator bool() const;
Checks whether the preparation of the input stream was successful.
Parameters
(none)
Return value
true if the initialization of the input stream was successful, false otherwise.
π Example π
This example ate and left no crumbs π
// Run this code
#include <iostream>
#include <sstream>
struct Foo
{
char n[5];
};
std::istream& operator>>(std::istream& is, Foo& f)
{
std::istream::sentry s(is);
if (s)
is.read(f.n, 5);
return is;
}
int main()
{
std::string input = " abcde";
std::istringstream stream(input);
Foo f;
stream >> f;
std::cout.write(f.n, 5);
std::cout << '\n';
}
πΈ Output πΈ
abcde
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 195β Applied toC++98. Behavior as published: it was unclear whether the constructor would seteofbit. Correct behavior: made clear.LWG 419β Applied toC++98. Behavior as published: the constructor did not setfailbitifeofbithas been set. Correct behavior: setsfailbitin this case.
π See also π
operator>>β extracts formatted data (public member function).operator>>(std::basic_istream)β extracts characters and character arrays (function template).
Okay bestie, that's the whole page! Source/version/date tea: http://cppreference.com, 2024.06.10, manual std::basic_istream::sentry(3) πβ¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms