std::basic_ostream<CharT,Traits>::sentry

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.

# Declarations

class sentry;

# Parameters

# Return value

true if the preparation of the output stream was successful, false otherwise.

# Example

#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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 397C++98the destructor might call os.flush(), which may throw exceptionsthe exception is not propagated
LWG 442C++98operator bool was not declared const (it is const in the synopsis)added const
LWG 835C++98if os sets unitbuf, the destructor would call os.flush(), whichis an UnformattedOutputFunction and creates another sentry object(whose destructor then creates another sentry object and so on)callsos.rdbuf()->pubsync()in this case instead

# See also