C++ named requirements: FormattedOutputFunction
目录 |
要求
一个 格式化输出函数 是 std::basic_ostream 的成员函数,或是涉及 std::basic_ostream 的非成员函数,该函数执行以下操作:
- 构造一个类型为 std::basic_ostream::sentry 的 sentry 对象。
- 若 bool ( sentry ) 为 true ,则尝试通过向输出流插入字符来执行期望的输出操作,具体实现方式类似于调用 rdbuf ( ) - > sputc ( ) 。也可使用 std::basic_ostream 的其他公共成员,但除 overflow() 、 xsputn() 和 sync() 外, rdbuf ( ) 的虚成员永远不会被调用。
- 销毁 sentry 并返回 * this 。
如果 sentry 构造失败,或 bool ( sentry ) 为 false ,则不会产生任何输出。
如果无法生成输出,将调用 setstate ( std:: ios_base :: failbit ) ,该操作可能抛出异常。
如果在输出过程中抛出异常,则会在
*
this
的错误状态中设置
std::
ios_base
::
badbit
,但不会引发
std::ios_base::failure
异常。如果该流的
异常掩码
中启用了对
badbit
的异常捕获(即
(
exceptions
(
)
&
badbit
)
!
=
0
),则该异常会被再次抛出。
当从格式化输出函数抛出异常时, sentry 也会在离开函数前被销毁。
填充
|
格式化输出函数根据 std::num_put::do_put() 阶段3确定填充方式。 |
(C++14 前) |
|
若流 os 的格式化输出函数确定填充,则其操作如下。
给定
若 ( os. flags ( ) & std:: ios_base :: adjustfield ) == std:: ios_base :: left 为 true ,则填充字符置于字符序列之后;否则,置于字符序列之前。 |
(C++14 起) |
标准库
以下标准库函数属于 格式化输出函数 。
- basic_ostream::operator<< (std::basic_ostream&, int / long / double / void* / bool)
- operator<< (std::basic_ostream&, char)
- operator<< (std::basic_ostream&, char*)
- operator<< (std::basic_ostream&, const std::bitset&)
- operator<< (std::basic_ostream&, const std::basic_string&)
-
operator<<,当调用 std::put_money 的返回值时
| (自 C++17 起) |
|
(C++23 起) |
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用标准 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 160 | C++98 |
判断捕获的异常是否被重新抛出的过程
提及了不存在的函数
exception()
|
更正为 exceptions() |
| LWG 165 | C++98 |
允许在
rdbuf
(
)
上调用的
唯一虚成员是
overflow()
|
同时允许调用
xsputn()
和
sync()
|