operator<< (std::basic_ostream)
|
定义于头文件
<ostream>
|
||
|
basic_ostream 与字符
|
||
| (1) | ||
|
template
<
class
CharT,
class
Traits
>
basic_ostream
<
CharT, Traits
>
&
|
||
|
template
<
class
CharT,
class
Traits
>
basic_ostream
<
CharT, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
basic_ostream 与字符数组
|
||
| (2) | ||
|
template
<
class
CharT,
class
Traits
>
basic_ostream
<
CharT, Traits
>
&
|
||
|
template
<
class
CharT,
class
Traits
>
basic_ostream
<
CharT, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
basic_ostream 右值
|
||
|
template
<
class
Ostream,
class
T
>
Ostream && operator << ( Ostream && os, const T & value ) ; |
(3) | (C++11 起) |
|
basic_ostream 与 UTF 字符/数组的已删除重载
|
||
| (4) | (C++20 起) | |
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream < char</span |
插入一个字符或字符串。
- 若 os. width ( ) > 1 ,则向输出字符添加 os. width ( ) - 1 个 os. fill ( ) 的副本以构成输出字符序列。
- 若 ( out. flags ( ) & std:: ios_base :: adjustfield ) == std:: ios_base :: left ,填充字符置于输出字符之后,否则置于之前。
-
对于第一和第三个重载(当
CharT与 ch 类型匹配时),精确插入 traits :: length ( s ) 个字符。 - 对于第二个重载,精确插入 std:: char_traits < char > :: length ( s ) 个字符。
- 对于最后两个重载,精确插入 traits :: length ( reinterpret_cast < const char * > ( s ) ) 个字符。
- 如果要插入的字符数小于 os. width ( ) ,则向字符序列添加足够数量的 os. fill ( ) 副本,使其长度等于 os. width ( ) 。
- 如果 ( out. flags ( ) & std:: ios_base :: adjustfield ) == std:: ios_base :: left ,填充字符将添加到输出序列的末尾,否则将添加到输出序列之前。
Ostream
是公开且明确继承自
std::ios_base
的类类型时,才参与重载决议。
目录 |
参数
| os | - | 用于插入数据的输出流 |
| ch | - | 待插入字符的引用 |
| s | - | 待插入字符字符串的指针 |
返回值
说明
在 LWG issue 1203 解决之前,诸如 ( std:: ostringstream ( ) << 1.2 ) . str ( ) 这样的代码无法通过编译。
说明: 1. 保留了所有HTML标签和属性 2. 未翻译
标签内的C++代码
3. 专业术语保持原样(如LWG issue、std::ostringstream、str()等)
4. 准确传达了技术含义:该代码示例在LWG 1203解决前存在编译问题
示例
#include <fstream> #include <iostream> void foo() { // 错误:operator<< (basic_ostream<char, _Traits>&, char8_t) 已被删除 // std::cout << u8'z' << '\n'; } std::ostream& operator<<(std::ostream& os, char8_t const& ch) { return os << static_cast<char>(ch); } int main() { std::cout << "Hello, world" // 使用 `const char*` 重载 << '\n'; // 使用 `char` 重载 std::ofstream{"test.txt"} << 1.2; // 使用右值重载 std::cout << u8'!' << '\n'; // 使用程序自定义的 operator<<(os, char8_t const&) }
输出:
Hello, world !
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用标准 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 167 | C++98 |
所有
(2)
中重载函数插入的字符数均为
traits :: length ( s ) |
更新了当
CharT
与
ch
类型不匹配时的重载数值
|
| LWG 1203 | C++11 | 右值流重载返回基类的左值引用 | 返回派生类的右值引用 |
| LWG 2011 | C++98 | 填充由 std::num_put::do_put() 决定 | 由操作符自身决定 |
| LWG 2534 | C++11 | 右值流重载未受约束 | 已受约束 |
参见
|
插入格式化数据
(公开成员函数) |
|
|
(C++23)
|
输出参数的
格式化
表示
(函数模板) |
|
扩展字符
(
std::basic_ios<CharT,Traits>
的公开成员函数)
|