std::basic_ostream::sentry(3)
π std::basic_ostream::sentry π
hey bestie, this is the std::basic_ostream::sentry page from the C++ Standard Libary manual (yes, the header says βLibaryβ and we're keeping it π
). it's giving bodyguard-for-your-output-stream energy, and honestly? iconic.
π NAME π
std::basic_ostream::sentry - std::basic_ostream::sentry
that's the name, short and slay. no notes. π
πΈ Synopsis πΈ
class sentry;
okay so picture this: an object of class basic_ostream::sentry is constructed in local scope at the beginning of each member function of std::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. no cap, that's the whole vibe: prep in the constructor, cleanup in the destructor. π«Ά
π Member functions π
- constructor β constructs the sentry object. All the preparation tasks are done here (public member function).
- 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).
π οΈ std::basic_ostream::sentry::sentry
explicit sentry( std::basic_ostream<CharT, Traits>& os );
Prepares the stream for formatted output.
If os.good() is false, returns. Otherwise, if os.tie() is not a null pointer, calls os.tie()->flush() to synchronize the output sequence with external streams. During preparation, the constructor may call setstate(failbit) (which may throw std::ios_base::failure).
If after preparation is completed, os.good() == true, then any subsequent calls to operator bool will return true. periodt. β
Parameters
osβ output stream to prepare.
Exceptions
std::ios_base::failure if the end of file condition occurs.
π§Ή std::basic_ostream::sentry::~sentry
~sentry();
If (os.flags() & std::ios_base::unitbuf) && !std::uncaught_exception() && os.good() is true, calls os.rdbuf()->pubsync(). If that function returns -1, sets badbit in os.rdstate() without propagating an exception. lowkey dramatic, but that's the assignment. π
β std::basic_ostream::sentry::operator bool
explicit operator bool() const;
Checks whether the preparation of the output stream was successful.
Parameters
(none) β literally none, bestie.
Return value
true if the preparation of the output stream was successful, false otherwise.
π§ͺ Example π§ͺ
// Run this code
#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
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. history really said βlet's fix thatβ and honestly? we love growth. π±
- LWG 397 β Applied to:
C++98.- Behavior as published: the destructor might call
os.flush(), which may throw exceptions. - Correct behavior: the exception is not propagated.
- Behavior as published: the destructor might call
- LWG 442 β Applied to:
C++98.- Behavior as published:
operator boolwas not declaredconst. - Correct behavior:
const(it is addedconstin the synopsis).
- Behavior as published:
- LWG 835 β Applied to:
C++98.- Behavior as published: if
ossetsunitbuf, the destructor would callos.flush(), which is anUnformattedOutputFunctionand creates another sentry object (whose destructor then creates another sentry object and so on). - Correct behavior: calls
os.rdbuf()->pubsync()in this case instead.
- Behavior as published: if
π See also π
operator<<β inserts formatted data (public member function).
okay that's the whole page, bestie! source is http://cppreference.com, version/date is 2024.06.10, and the page is std::basic_ostream::sentry(3). mwah πβ¨
π¦ 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