public member class
<ostream> <iostream>

std::basic_ostream::sentry

class sentry;
Prepare stream for output
Member class that performs a series of operations before and after each output operation:

Its constructor performs the following operations on the stream object passed as its argument (in the same order):
  • If any of its internal error flags is set, the function may set its failbit flag and return.
  • If it is a tied stream, the function flushes the stream it is tied to (if its output buffer is not empty), unless the function determines that synchronization is not necessary.
    On failure, it may set the failbit flag.

Its destructor performs the following operations on the same stream object (in the same order):

Implementations may use the construction and destruction of sentry objects to perform additional initialization or cleanup operations on the stream common to all output operations.

All member functions that perform an output operation automatically construct an object of this class and then evaluate it (which returns true if no state flag was set). Only if this object evaluates to true, the function attempts the output operation (otherwise, it returns without performing it). Before returning, the function destroys the sentry object.

The structure of this class is:
1
2
3
4
5
6
7
8
9
class sentry {
public:
  explicit sentry (basic_ostream& os);
  ~sentry();
  operator bool() const;
private:
  sentry (const sentry&);             // not defined
  sentry& operator= (const sentry&);  // not defined
};

1
2
3
4
5
6
7
8
class sentry {
public:
  explicit sentry (basic_ostream& os);
  ~sentry();
  explicit operator bool() const;
  sentry (const sentry&) = delete;
  sentry& operator= (const sentry&) = delete;
};


Members

explicit sentry (basic_ostream& os);
Prepares the output stream for an output operation, performing the actions described above.
~sentry();
Performs the operations described above.
explicit operator bool() const;
When the object is evaluated, it returns a bool value indicating whether the sentry constructor successfully performed all its tasks: If at some point of the construction process, an internal error flags was set, this function always returns false for that object.

See also

<ostream>