std::basic_ostream<CharT,Traits>:: operator<<
|
basic_ostream
&
operator
<<
(
bool
value
)
;
|
(1) | |
|
basic_ostream
&
operator
<<
(
long
value
)
;
|
(2) | |
|
basic_ostream
&
operator
<<
(
unsigned
long
value
)
;
|
(3) | |
|
basic_ostream
&
operator
<<
(
long
long
value
)
;
|
(4) | (自 C++11 起) |
|
basic_ostream
&
operator
<<
(
unsigned
long
long
value
)
;
|
(5) | (自 C++11 起) |
|
basic_ostream
&
operator
<<
(
double
value
)
;
|
(6) | |
|
basic_ostream
&
operator
<<
(
long
double
value
)
;
|
(7) | |
|
basic_ostream
&
operator
<<
(
const
void
*
value
)
;
|
(8) | |
|
basic_ostream
&
operator
<<
(
const
volatile
void
*
value
)
;
|
(9) | (自 C++23 起) |
|
basic_ostream
&
operator
<<
(
std::
nullptr_t
)
;
|
(10) | (自 C++17 起) |
|
basic_ostream
&
operator
<<
(
short
value
)
;
|
(11) | |
|
basic_ostream
&
operator
<<
(
int
value
)
;
|
(12) | |
|
basic_ostream
&
operator
<<
(
unsigned
short
value
)
;
|
(13) | |
|
basic_ostream
&
operator
<<
(
unsigned
int
value
)
;
|
(14) | |
|
basic_ostream
&
operator
<<
(
float
value
)
;
|
(15) | |
|
basic_ostream
&
operator
<<
(
/* 扩展浮点类型 */
value
)
;
|
(16) | (自 C++23 起) |
|
basic_ostream
&
operator
<<
(
std::
basic_streambuf
<
CharT, Traits
>
*
sb
)
;
|
(17) | |
|
basic_ostream
&
operator
<<
(
std:: ios_base & ( * func ) ( std:: ios_base & ) ) ; |
(18) | |
|
basic_ostream
&
operator
<<
(
std:: basic_ios < CharT, Traits > & ( * func ) ( std:: basic_ios < CharT, Traits > & ) ) ; |
(19) | |
|
basic_ostream
&
operator
<<
(
std::
basic_ostream
<
CharT, Traits
>
&
(
*
func
)
|
(20) | |
向流中插入数据。
badbit
。
- 当 flags ( ) & std:: ios_base :: basefield 为 std::ios_base::oct 或 std::ios_base::hex 时,取 static_cast < long > ( static_cast < unsigned short > ( value ) ) ;
- 否则取 static_cast < long > ( value ) 。
- 当 flags ( ) & std:: ios_base :: basefield 为 std::ios_base::oct 或 std::ios_base::hex 时,取 static_cast < long > ( static_cast < unsigned int > ( value ) ) ;
- 否则取 static_cast < long > ( value ) 。
- 若该等级小于等于 double ,则按 (6) 的方式插入 static_cast < double > ( value ) 。
- 否则,若该等级小于等于 long double ,则按 (7) 的方式插入 static_cast < long double > ( value ) 。
- 否则,此重载的调用在条件性支持的情况下具有实现定义的语义。
-
- 输入序列遇到文件结尾;
- 输出序列插入失败(此时待插入字符不会被提取);
- 发生异常(此时异常会被捕获)。
failbit
,且若
failbit
在
exceptions()
中被设置,则重新抛出该异常。
目录 |
参数
| value | - | 要插入的整型、浮点型、布尔型或指针值 |
| func | - | 要调用的函数 |
| sb | - | 指向要从中读取数据的流缓冲区的指针 |
返回值
注释
不存在针对指向非静态成员的指针 、指向volatile的指针 (直至C++23) 或函数指针的重载(除了那些被 ( 18-20 ) 重载接受的签名类型)。
-
尝试输出此类对象会触发向
bool
的隐式转换,对于任何非空指针值,将输出数值
1
(除非设置了
boolalpha,此时将输出 true )。
字符和字符串参数(例如类型为 char 或 const char * )由 operator << 的 非成员重载 处理。
- 尝试使用成员函数调用语法输出字符(例如, std:: cout . operator << ( 'c' ) ; )将调用 ( 2-5 ) 或 ( 11-14 ) 中的重载版本并输出数值。
- 尝试使用成员函数调用语法输出字符串将调用重载版本 (8) 并打印指针值而非字符串内容。
重载 (10) 由 LWG 问题 2221 的决议所添加,但在 C++11/14 模式下从未在任何标准库实现中得到实现。
示例
#include <iomanip> #include <iostream> #include <sstream> int fun() { return 42; } int main() { std::istringstream input(" \"Some text.\" "); double f = 3.14; bool b = true; std::cout << fun() // int 重载 (12) << ' ' // 非成员函数重载 << std::boolalpha // 函数重载 (18) << b // bool 重载 (1) << " " // 非成员函数重载 << std::fixed // 函数重载 (18) 再次调用 << f // double 重载 (6) << input.rdbuf() // streambuf 重载 << fun // bool 重载 (1): 不存在 int(*)() 的重载版本 << std::endl; // 函数重载 (18) 再次调用 int x = 0; int* p1 = &x; volatile int* p2 = &x; std::cout << "p1: " << p1 << '\n' // `const void*` 重载,输出地址 << "p2: " << p2 << '\n'; // C++23 前 (P1147): bool 重载 :),因为 // operator<<(const void*) 不匹配,它会丢弃 `volatile` // 限定符。为解决此问题,C++23 新增了 `const volatile void*` 重载 (9), // 按预期输出地址。 }
可能的输出:
42 true 3.140000 "Some text." true p1: 0x7ffcea766600 p2: 0x7ffcea766600
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 117 | C++98 |
重载
(1-8,11-15)
将插入操作委托给
num_put::put ,但该函数没有针对 short 、 unsigned short 、 int 、 unsigned int 和 float 的重载版本 |
在传递给
num_put::put
之前
先进行类型转换 |
| LWG 567 | C++98 |
重载
(17)
因
LWG 问题 60
的解决方案
而表现为 FormattedOutputFunction |
表现为
UnformattedOutputFunction |
参见
|
插入字符数据或插入到右值流
(函数模板) |
|
|
对字符串执行流输入和输出
(函数模板) |
|
|
(C++17)
|
对字符串视图执行流输出
(函数模板) |
|
对位集执行流输入和输出
(函数模板) |
|
|
序列化和反序列化复数
(函数模板) |
|
|
(C++11)
|
对伪随机数引擎执行流输入和输出
(函数模板) |
|
(C++11)
|
对伪随机数分布执行流输入和输出
(函数模板) |
|
插入一个字符
(公开成员函数) |
|
|
插入字符块
(公开成员函数) |
|
|
(C++17)
|
将整数或浮点数值转换为字符序列
(函数) |