Namespaces
Variants

std:: basic_osyncstream

From cppreference.net
< 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 >
(C++20 起)

类模板 std::basic_osyncstream std::basic_syncbuf 的便捷封装。它为向同一流写入数据的线程提供了同步机制。

它可以与命名变量一起使用:

{
    std::osyncstream synced_out(std::cout); // std::cout 的同步包装器
    synced_out << "Hello, ";
    synced_out << "World!";
    synced_out << std::endl; // 记录了刷新操作,但尚未执行
    synced_out << "and more!\n";
} // 字符被传输且 std::cout 被刷新

以及使用临时对象:

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

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

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

cpp/io/ios base cpp/io/basic ios cpp/io/basic ostream std-basic osyncstream-inheritance.svg

继承关系图

提供了几种常见字符类型的类型定义:

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

目录

成员类型

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

成员函数

构造 basic_osyncstream 对象
(公开成员函数)
赋值 basic_osyncstream 对象
(公开成员函数)
销毁 basic_osyncstream 并发送其内部缓冲区内容
(公开成员函数)
获取指向底层 basic_syncbuf 的指针
(公开成员函数)
获取指向最终目标流缓冲区的指针
(公开成员函数)
调用底层 basic_syncbuf emit() 以将其内部数据传输至最终目标
(公开成员函数)

继承自 std:: basic_ostream

成员函数

格式化输出
插入格式化数据
std::basic_ostream<CharT,Traits> 的公开成员函数)
非格式化输出
插入单个字符
std::basic_ostream<CharT,Traits> 的公开成员函数)
插入字符块
std::basic_ostream<CharT,Traits> 的公开成员函数)
定位操作
返回输出位置指示器
std::basic_ostream<CharT,Traits> 的公开成员函数)
设置输出位置指示器
std::basic_ostream<CharT,Traits> 的公开成员函数)
其他操作
与底层存储设备同步
std::basic_ostream<CharT,Traits> 的公开成员函数)

成员类

实现为流输出操作做准备的基础逻辑
std::basic_ostream<CharT,Traits> 的公开成员类)

继承自 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> 的公开成员函数)
检查是否到达文件末尾
std::basic_ios<CharT,Traits> 的公开成员函数)
检查是否发生错误
std::basic_ios<CharT,Traits> 的公开成员函数)
检查是否发生不可恢复错误
std::basic_ios<CharT,Traits> 的公开成员函数)
检查是否发生错误( fail() 的同义词)
std::basic_ios<CharT,Traits> 的公开成员函数)
检查是否未发生错误( ! fail() 的同义词)
std::basic_ios<CharT,Traits> 的公开成员函数)
返回状态标志
std::basic_ios<CharT,Traits> 的公开成员函数)
设置状态标志
std::basic_ios<CharT,Traits> 的公开成员函数)
修改状态标志
std::basic_ios<CharT,Traits> 的公开成员函数)
格式化
复制格式化信息
std::basic_ios<CharT,Traits> 的公开成员函数)
管理填充字符
std::basic_ios<CharT,Traits> 的公开成员函数)
杂项
管理异常掩码
std::basic_ios<CharT,Traits> 的公开成员函数)
设置本地化环境
std::basic_ios<CharT,Traits> 的公开成员函数)
管理关联的流缓冲区
std::basic_ios<CharT,Traits> 的公开成员函数)
管理绑定的流
std::basic_ios<CharT,Traits> 的公开成员函数)
字符窄化
std::basic_ios<CharT,Traits> 的公开成员函数)
字符宽化
std::basic_ios<CharT,Traits> 的公开成员函数)

继承自 std:: ios_base

成员函数

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

成员类型与常量

类型 说明
流打开模式类型

同时定义以下常量:

常量 说明
app 每次写入前定位到流末尾
binary 二进制模式 打开
in 为读取而打开
out 为写入而打开
trunc 打开时清空流内容
ate 打开后立即定位到流末尾
noreplace (C++23) 以独占模式打开

(类型定义)
格式化标志类型

同时定义以下常量:

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

(类型定义)
流状态类型

同时定义以下常量:

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

(类型定义)
寻址方向类型

同时定义以下常量:

常量 说明
beg 流的起始位置
end 流的结束位置
cur 流位置指示器的当前位置

(类型定义)
指定事件类型
(枚举)
回调函数类型
(typedef)

注释

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