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 output | ||||
Unformatted output | ||||
Positioning | ||||
Miscellaneous | ||||
(C++11) | ||||
Member classes | ||||
basic_ostream::sentry | ||||
Non-member functions | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
class sentry; | ||
An object of classbasic_ostream::sentry
is constructed in local scope at the beginning of each member function ofstd::basic_ostream that performs output (both formatted and unformatted). Its constructor prepares the output stream: checks if the stream is already in a failed state, flushes the tie()'d output streams, and performs other implementation-defined tasks if necessary. Implementation-defined cleanup, as well as flushing of the output stream if necessary, is performed in the destructor, so that it is guaranteed to happen if exceptions are thrown during output.
Contents |
(constructor) | constructs the sentry object. All the preparation tasks are done here (public member function)[edit] |
(destructor) | finalizes the stream object after formatted output or after exception, if necessary (public member function) |
operator= | the assignment operator is deleted (public member function) |
operator bool | checks if the preparation of the stream object was successful (public member function)[edit] |
explicit sentry(std::basic_ostream<CharT, Traits>& os); | ||
Prepares the stream for formatted output.
Ifos.good() isfalse, returns. Otherwise, ifos.tie() is not a null pointer, callsos.tie()->flush() to synchronize the output sequence with external streams. During preparation, the constructor may callsetstate(failbit) (which may throwstd::ios_base::failure).
If after preparation is completed,os.good()==true, then any subsequent calls tooperatorbool will returntrue.
os | - | output stream to prepare |
std::ios_base::failure if the end of file condition occurs.
~sentry(); | ||
If(os.flags()&std::ios_base::unitbuf)&&!std::uncaught_exception()&& os.good()) istrue, callsos.rdbuf()->pubsync(). If that function returns-1, setsbadbit inos.rdstate() without propagating an exception.
explicit operatorbool()const; | ||
Checks whether the preparation of the output stream was successful.
(none)
true if the preparation of the output stream was successful,false otherwise.
#include <iostream>#include <sstream> struct Foo{char n[6];}; std::ostream& operator<<(std::ostream& os, Foo& f){ std::ostream::sentry s(os);if(s) os.write(f.n,5);return os;} int main(){ Foo f={"abcde"};std::cout<< f<<'\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 397 | C++98 | the destructor might callos.flush(), which may throw exceptions | the exception is not propagated |
LWG 442 | C++98 | operatorbool was not declaredconst (it isconst in thesynopsis) | addedconst |
LWG 835 | C++98 | ifos setsunitbuf , the destructor would callos.flush(), whichis anUnformattedOutputFunction and creates another sentry object (whose destructor then creates another sentry object and so on) | calls os.rdbuf()->pubsync() in this case instead |
inserts formatted data (public member function)[edit] |