I/O manipulators | ||||
Print functions(C++23) | ||||
C-style I/O | ||||
Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
Streams | ||||
Abstractions | ||||
File I/O | ||||
String I/O | ||||
Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
Synchronized Output | ||||
(C++20) | ||||
Types | ||||
Error category interface | ||||
(C++11) | ||||
(C++11) |
Global objects | ||||
Member functions | ||||
(C++11) | ||||
Formatted input | ||||
Unformatted input | ||||
Positioning | ||||
Miscellaneous | ||||
(C++11) | ||||
Member classes | ||||
basic_istream::sentry | ||||
Non-member functions | ||||
class sentry; | ||
An object of classbasic_istream::sentry
is constructed in local scope at the beginning of each member function ofstd::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 unlessnoskipws 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.
Contents |
traits_type | Traits |
(constructor) | constructs the sentry object. All the preparation tasks are done here (public member function)[edit] |
(destructor) | finalizes the stream object after formatted input or after exception, if necessary (public member function)[edit] |
operator= [deleted] | not copy assignable (public member function) |
operator bool | checks if the preparation of the stream object was successful (public member function)[edit] |
explicit sentry(std::basic_istream<CharT, Traits>& is,bool noskipws=false); | ||
Prepares the stream for formatted input.
Ifis.good() isfalse, callsis.setstate(std::ios_base::failbit) and returns. Otherwise, ifis.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.
Ifnoskipws is zero andis.flags()&std::ios_base::skipws is 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 callsetstate(std::ios_base::failbit) (which may throwstd::ios_base::failure).
If after preparation is completed,is.good()==true, then any subsequent calls tooperatorbool will returntrue.
is | - | input stream to prepare |
noskipws | - | true if whitespace should not be skipped |
std::ios_base::failure if the end of file condition occurs when skipping whitespace.
~sentry(); | ||
Does nothing.
explicit operatorbool()const; | ||
Checks whether the preparation of the input stream was successful.
(none)
true if the initialization of the input stream was successful,false otherwise.
#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
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 195 | C++98 | it was unclear whether the constructor would seteofbit | made clear |
LWG 419 | C++98 | the constructor did not setfailbit ifeofbit has been set | setsfailbit in this case |
extracts formatted data (public member function)[edit] | |
extracts characters and character arrays (function template)[edit] |