Files
2025-10-25 03:02:53 +03:00

81 lines
3.0 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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.4Flags 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)