命名空间
变体
操作

std::basic_osyncstream

来自 cppreference.cn
< cpp‎ | io
定义于头文件 <syncstream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>

> class basic_osyncstream : public std::basic_ostream<CharT, Traits>
(since C++20)

类模板 std::basic_osyncstreamstd::basic_syncbuf 的便捷包装器。它提供了一种机制来同步写入同一流的线程。

它可以与具名变量一起使用

{
    std::osyncstream synced_out(std::cout); // synchronized wrapper for std::cout
    synced_out << "Hello, ";
    synced_out << "World!";
    synced_out << std::endl; // flush is noted, but not yet performed
    synced_out << "and more!\n";
} // characters are transferred and std::cout is flushed

以及临时变量

std::osyncstream(std::cout) << "Hello, " << "World!" << '\n';

它保证了对同一最终目标缓冲区(在上面的示例中为 std::cout)的所有输出都将免受数据竞争,并且不会以任何方式交错或损坏,只要对该最终目标缓冲区的每次写入都是通过 std::basic_osyncstream 的(可能不同的)实例进行的。

std::basic_osyncstream 的典型实现仅包含一个成员:包装的 std::basic_syncbuf

cpp/io/ios basecpp/io/basic ioscpp/io/basic ostreamstd-basic osyncstream-inheritance.svg

继承图

提供了几种用于常见字符类型的 typedef

定义于头文件 <syncstream>
类型 定义
std::osyncstream std::basic_osyncstream<char>
std::wosyncstream std::basic_osyncstream<wchar_t>

内容

[edit] 成员类型

成员类型 定义
char_type CharT[edit]
traits_type Traits; 如果 Traits::char_type 不是 CharT,则程序是非良构的。[edit]
int_type Traits::int_type[edit]
pos_type Traits::pos_type[edit]
off_type Traits::off_type[edit]
allocator_type Allocator
streambuf_type std::basic_streambuf<CharT, Traits>
syncbuf_type std::basic_syncbuf<CharT, Traits, Allocator>

[edit] 成员函数

构造一个 basic_osyncstream 对象
(公共成员函数) [edit]
赋值一个 basic_osyncstream 对象
(公共成员函数) [edit]
销毁 basic_osyncstream 并发出其内部缓冲区
(公共成员函数) [edit]
获取指向底层 basic_syncbuf 的指针
(公共成员函数) [edit]
获取指向最终目标流缓冲区的指针
(公共成员函数) [edit]
在底层 basic_syncbuf 上调用 emit(),以将其内部数据传输到最终目标
(公共成员函数) [edit]

继承自 std::basic_ostream

成员函数

格式化输出
插入格式化数据
(std::basic_ostream<CharT,Traits> 的公共成员函数) [edit]
非格式化输出
插入一个字符
(std::basic_ostream<CharT,Traits> 的公共成员函数) [edit]
插入字符块
(std::basic_ostream<CharT,Traits> 的公共成员函数) [edit]
定位
返回输出位置指示器
(std::basic_ostream<CharT,Traits> 的公共成员函数) [edit]
设置输出位置指示器
(std::basic_ostream<CharT,Traits> 的公共成员函数) [edit]
杂项
与底层存储设备同步
(std::basic_ostream<CharT,Traits> 的公共成员函数) [edit]

成员类

实现用于准备流以进行输出操作的基本逻辑
(std::basic_ostream<CharT,Traits> 的公共成员类) [edit]

继承自 std::basic_ios

成员类型

成员类型 定义
char_type CharT
traits_type Traits
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type

成员函数

状态函数
检查是否没有错误发生,即 I/O 操作是否可用
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
检查是否已到达文件末尾
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
检查是否发生了错误
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
检查是否发生了不可恢复的错误
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
检查是否发生了错误(fail() 的同义词)
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
检查是否没有错误发生(!fail() 的同义词)
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
返回状态标志
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
设置状态标志
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
修改状态标志
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
格式化
复制格式化信息
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
管理填充字符
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
杂项
管理异常掩码
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
设置区域设置
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
管理关联的流缓冲区
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
管理绑定的流
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
窄化字符
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]
宽化字符
(std::basic_ios<CharT,Traits> 的公共成员函数) [edit]

继承自 std::ios_base

成员函数

格式化
管理格式标志
(std::ios_base 的公共成员函数) [edit]
设置特定格式标志
(std::ios_base 的公共成员函数) [edit]
清除特定格式标志
(std::ios_base 的公共成员函数) [edit]
管理浮点运算的十进制精度
(std::ios_base 的公共成员函数) [edit]
管理字段宽度
(std::ios_base 的公共成员函数) [edit]
区域设置
设置区域设置
(std::ios_base 的公共成员函数) [edit]
返回当前区域设置
(std::ios_base 的公共成员函数) [edit]
内部可扩展数组
[静态]
返回一个程序范围唯一的整数,该整数可以安全地用作 pword()iword() 的索引
(std::ios_base 的公共静态成员函数) [edit]
如有必要,调整私有存储的大小,并访问给定索引处的 long 元素
(std::ios_base 的公共成员函数) [edit]
如有必要,调整私有存储的大小,并访问给定索引处的 void* 元素
(std::ios_base 的公共成员函数) [edit]
杂项
注册事件回调函数
(std::ios_base 的公共成员函数) [edit]
设置 C++ 和 C I/O 库是否可互操作
(std::ios_base 的公共静态成员函数) [edit]
成员类
流异常
(std::ios_base 的公共成员类) [edit]
初始化标准流对象
(std::ios_base 的公共成员类) [edit]

成员类型和常量

类型 解释
流打开模式类型

以下常量也被定义

常量 解释[edit]
app 每次写入前查找流的末尾[edit]
binary 二进制模式打开[edit]
in 打开以进行读取[edit]
out 打开以进行写入[edit]
trunc 打开时丢弃流的内容[edit]
ate 打开后立即查找流的末尾[edit]
noreplace (C++23) 以独占模式打开[edit]

(typedef) [edit]
格式化标志类型

以下常量也被定义

常量 解释[edit]
dec 对整数 I/O 使用十进制基数:参见 std::dec[edit]
oct 对整数 I/O 使用八进制基数:参见 std::oct[edit]
hex 对整数 I/O 使用十六进制基数:参见 std::hex[edit]
basefield dec | oct | hex。用于掩码操作[edit]
left 左对齐(向右添加填充字符):参见 std::left[edit]
right 右对齐(向左添加填充字符):参见 std::right[edit]
internal 内部对齐(在内部指定点添加填充字符):参见 std::internal[edit]
adjustfield left | right | internal。用于掩码操作[edit]
scientific 使用科学计数法生成浮点类型,如果与 fixed 结合使用,则使用十六进制计数法:参见 std::scientific[edit]
fixed 使用定点计数法生成浮点类型,如果与 scientific 结合使用,则使用十六进制计数法:参见 std::fixed[edit]
floatfield scientific | fixed。用于掩码操作[edit]
boolalpha 以字母数字格式插入和提取 bool 类型:参见 std::boolalpha[edit]
showbase 生成指示整数输出的数字基数的前缀,货币 I/O 中需要货币指示符:参见 std::showbase[edit]
showpoint 无条件地为浮点数输出生成小数点字符:参见 std::showpoint[edit]
showpos 为非负数输出生成 + 字符:参见 std::showpos[edit]
skipws 在某些输入操作之前跳过前导空白:参见 std::skipws[edit]
unitbuf 在每次输出操作后刷新输出:参见 std::unitbuf[edit]
uppercase 在某些输出操作中,用等效的大写字母替换某些小写字母:参见 std::uppercase[edit]

(typedef) [edit]
流状态类型

以下常量也被定义

常量 解释[edit]
goodbit 没有错误[edit]
badbit 不可恢复的流错误[edit]
failbit 输入/输出操作失败(格式化或提取错误)[edit]
eofbit 关联的输入序列已到达文件末尾[edit]

(typedef) [edit]
查找方向类型

以下常量也被定义

常量 解释[edit]
beg 流的开始[edit]
end 流的结尾[edit]
cur 流位置指示器的当前位置[edit]

(typedef) [edit]
指定事件类型
(enum) [edit]
回调函数类型
(类型别名) [编辑]

[编辑] 注释

特性测试 标准 特性
__cpp_lib_syncbuf 201803L (C++20) 同步缓冲输出流 (std::osyncstream, std::syncbuf) 和操纵符