首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_ostream::sentry

class sentry;

?

?

类对象basic_ostream::sentry的每个成员函数开始时,在局部范围内构造std::basic_ostream它执行输出%28,包括格式化和未格式化的%29。它的构造函数准备输出流:检查流是否已经处于失败状态,刷新Tie%28%29%27D输出流,并在必要时执行其他实现定义的任务。在析构函数中执行实现定义的清理以及必要时对输出流的刷新,以便保证在输出期间抛出异常时会发生。

成员函数

(constructor)

constructs the sentry object. All the preparation tasks are done here (public member function)

(destructor)

finalizes the stream object after formatted output or after exception, if necessary (public member function)

operator=

the assignment operator is deleted (public member function)

operator bool

checks if the preparation of the stream object was successful (public member function)

性病:基本[医]哨兵::哨兵

explicit sentry( std::basic_ostream<CharT,Traits>& os );

?

?

为格式化输出准备流。

如果os.good()false,返回。否则,如果os.tie()不是空指针,调用os.tie()->flush()将输出序列与外部流同步。在准备过程中,构造函数可以调用setstate(failbit)%28std::ios_base::failure29%。

如果准备完成后,is.good() == true,然后任何后续的调用operator bool会回来true...

参数

os

-

output stream to prepare

例外

std::ios_base::failure如果出现文件结束情况。

性病:基本[医]哨兵::~哨兵

~sentry();

?

?

如果(os.flags()&std::ios_base::unitbuf)&&!std::uncaught_exception()&& os.good())true,电话os.rdbuf()->pubsync().如果该函数返回-1、集badbitos.rdstate()而不传播异常。

性病:基本[医]Ostream::哨兵::操作者bool

explicit operator bool() const;

?

?

检查输出流的准备是否成功。

参数

%280%29

返回值

true如果输出流的准备成功,false否则。

二次

代码语言:javascript
复制
#include <iostream>
#include <sstream>
 
struct Foo
{
    char n[6];
};
 
std::ostream& operator<<(std::ostream& is, Foo& f)
{
    std::ostream::sentry s(is);
    if (s) {
        is.write(f.n, 5);
    }
    return is;
}
 
int main()
{
    Foo f = { "abcde" };
    std::cout << f << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
abcde

二次

另见

operator<<

inserts formatted data (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com