[ostream.sentry]

# 31 Input/output library [[input.output]](./#input.output)

## 31.7 Formatting and manipulators [[iostream.format]](iostream.format#ostream.sentry)

### 31.7.6 Output streams [[output.streams]](output.streams#ostream.sentry)

#### 31.7.6.2 Class template basic_ostream [[ostream]](ostream#sentry)

#### 31.7.6.2.4 Class basic_ostream​::​sentry [ostream.sentry]

[ð](#lib:basic_ostream::sentry)

namespace std {template<class charT, class traits>class basic_ostream<charT, traits>::sentry {bool *ok_*; // *exposition only*public:explicit sentry(basic_ostream& os); ~sentry(); explicit operator bool() const { return *ok_*; } sentry(const sentry&) = delete;
 sentry& operator=(const sentry&) = delete; };}

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6205)

The classsentry defines a class that is responsible for doing exception safe prefix and suffix
operations[.](#1.sentence-1)

[ð](#lib:basic_ostream::sentry,constructor)

`explicit sentry(basic_ostream& os);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6217)

Ifos.good() is nonzero, prepares for formatted or unformatted output[.](#2.sentence-1)

Ifos.tie() is not a null pointer, callsos.tie()->flush()[.](#2.sentence-2)[283](#footnote-283 "The call os.tie()->flush() does not necessarily occur if the function can determine that no synchronization is necessary.")

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6233)

If, after any preparation is completed,os.good() istrue,*ok_* == true otherwise,*ok_* == false[.](#3.sentence-1)

During preparation, the constructor may callsetstate(failbit) (which may throwios_base​::​​failure ([[iostate.flags]](iostate.flags "31.5.4.4 Flags functions")))[.](#3.sentence-2)[284](#footnote-284 "The sentry constructor and destructor can also perform additional implementation-dependent operations.")

[ð](#lib:basic_ostream::sentry,destructor)

`~sentry();
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6261)

If(os.flags() & ios_base​::​unitbuf) && !uncaught_exceptions()&& os.good() istrue,
callsos.rdbuf()->pubsync()[.](#4.sentence-1)

If that function returns â1 or
exits via an exception, sets badbit inos.rdstate() without propagating an exception[.](#4.sentence-2)

[ð](#lib:operator_bool,basic_ostream::sentry)

`explicit operator bool() const;
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L6279)

*Effects*: Returns*ok_*[.](#5.sentence-1)

[283)](#footnote-283)[283)](#footnoteref-283)

The callos.tie()->flush() does not necessarily occur if the function can determine that no
synchronization is necessary[.](#footnote-283.sentence-1)

[284)](#footnote-284)[284)](#footnoteref-284)

Thesentry constructor and destructor
can also perform additionalimplementation-dependent operations[.](#footnote-284.sentence-1)
