ostream::sentry


public member classes
class sentry;

Perform exception safe prefix/suffix operations

This member class performs during its construction a series of operations that prepare the stream object to perform output operations.

An object of this class is automatically constructed by all member functions that perform an output operation on the stream. After being constructed, these functions evaluate the sentry object, and only continue with their output operation if true is returned.

All these member functions destruct the sentry object before returning.

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


Members

constructor: explicit sentry ( ostream& os )
The constructor is in charge on performing the initial safety checks. Before proceeding, it checks whether none of the stream's internal error flags are set. If so, the constructor performs the appropiate operations to grant that the tied output stream is flushed before the output operation is performed on the stream.
If an error happens during this construction process, the failbit will be set, and an exception may be thrown if the member ios::exceptions is set to the appropiate value.
The private declarations of the copy constructor and assignment operator without definitions prevent constructed objects to be copied.
destructor: ~sentry
does nothing
operator bool() const
When the object is evaluated, it returns a boolean value indicating whether the output sequence is ready.


Basic template member declaration

( basic_ostream<charT,traits> )
1
2
3
4
5
6
7
8
9
10
class basic_ostream<charT,traits>::sentry {
  typedef traits traits_type;
public:
  explicit sentry ( basic_ostream<charT,traits>& os );
  ~sentry ();
  operator bool() const;
private:
  sentry (const sentry&);
  sentry& operator= (const sentry&);
};