std::basic_ostream<CharT,Traits>::operator<<
来自 cppreference.cn
< cpp | io | basic ostream
| 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<<( /* extended-floating-point-type */ 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) | |
将数据插入流中。
1-8) 插入 value。
此函数作为 FormattedOutputFunction 行为。构造并检查守卫对象后,通过调用 std::num_put::put() 插入值。如果在输出期间遇到文件尾条件 (put().failed() == true),则设置
badbit。9) 等价于 return operator<<(const_cast<const void*>(p));。
10) 等价于 return *this << s;,其中 s 是实现定义的以空字符结尾的字符类型字符串。
11) 插入 short value 的值。
此函数作为 FormattedOutputFunction 行为。构造并检查守卫对象后,插入 long 值 lval,如同在 (2) 中一样,其中 lval 是
- static_cast<long>(static_cast<unsigned short>(value)),如果 flags() & std::ios_base::basefield 是 std::ios_base::oct 或 std::ios_base::hex,或
- static_cast<long>(value),否则。
12) 插入 int value 的值。
此函数作为 FormattedOutputFunction 行为。构造并检查守卫对象后,插入 long 值 lval,如同在 (2) 中一样,其中 lval 是
- static_cast<long>(static_cast<unsigned int>(value)),如果 flags() & std::ios_base::basefield 是 std::ios_base::oct 或 std::ios_base::hex,或
- static_cast<long>(value),否则。
13,14) 插入 unsigned short 或 unsigned int value 的值。
15) 插入 float value 的值。
此函数作为 FormattedOutputFunction 行为。构造并检查守卫对象后,检查 /* extended-floating-point-type */ 的 浮点转换等级
- 如果等级小于或等于 double 的等级,则插入 static_cast<double>(value),如同在 (6) 中一样。
- 否则,如果等级小于或等于 long double 的等级,则插入 static_cast<long double>(value),如同在 (7) 中一样。
- 否则,此重载的调用是条件支持的,其语义由实现定义。
17) 此函数作为 UnformattedOutputFunction 行为。构造并检查守卫对象后,检查 sb 是否为空指针。如果是,则执行 setstate(badbit) 并退出。否则,从 sb 控制的输入序列中提取字符并插入到 *this,直到满足以下条件之一:
- 输入序列上发生文件尾;
- 输出序列中插入失败(在这种情况下,要插入的字符不会被提取);
- 发生异常(在这种情况下,异常被捕获)。
failbit,并且如果 exceptions() 中设置了 failbit,则重新抛出异常。目录 |
[编辑] 参数
| value | - | 要插入的整型、浮点型、布尔型或指针值 |
| func | - | 要调用的函数 |
| sb | - | 指向要从中读取数据的流缓冲区的指针 |
[编辑] 返回值
1-19) *this
20) func(*this)
[编辑] 注意
没有针对指向非静态成员的指针、指向 volatile 的指针,(直到 C++23) 或函数指针(除了 (18-20) 重载接受的签名)的重载。
- 尝试输出此类对象会调用隐式转换为 bool,对于任何非空指针值,都会打印值 1(除非设置了
boolalpha,在这种情况下会打印 true)。
字符和字符串参数(例如,类型为 char 或 const char*)由 operator<< 的非成员重载处理。
- 尝试使用成员函数调用语法输出字符(例如,std::cout.operator<<('c');)将调用 (2-5) 或 (11-14) 中的一个重载并输出数值。
- 尝试使用成员函数调用语法输出字符串将调用重载 (8) 并打印指针值。
重载 (10) 是通过 LWG issue 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 overload (12) << ' ' // non-member overload << std::boolalpha // function overload (18) << b // bool overload (1) << " " // non-member overload << std::fixed // function overload (18) again << f // double overload (6) << input.rdbuf() // streambuf overload << fun // bool overload (1): there's no overload for int(*)() << std::endl; // function overload (18) again int x = 0; int* p1 = &x; volatile int* p2 = &x; std::cout << "p1: " << p1 << '\n' // `const void*` overload, prints address << "p2: " << p2 << '\n'; // before C++23 (P1147): bool overload :), because // operator<<(const void*) is not a match, as it discards the `volatile` // qualifier. To fix this, C++23 adds `const volatile void*` overload (9), // that prints the address as expected. }
可能的输出
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) 曾作为 FormattedOutputFunction 行为 由于 LWG issue 60 的决议 |
它现在作为 UnformattedOutputFunction(非格式化输出函数) |
[编辑] 参阅
| 插入字符数据或插入到右值流中 (函数模板) | |
| 对字符串执行流输入和输出 (函数模板) | |
| (C++17) |
对 string_view 执行流输出 (函数模板) |
| 执行位集的流输入和输出 (函数模板) | |
| 序列化和反序列化复数 (函数模板) | |
| (C++11) |
对伪随机数引擎执行流输入和输出 (函数模板) |
| (C++11) |
对伪随机数分布执行流输入和输出 (函数模板) |
| 插入一个字符 (公共成员函数) | |
| 插入字符块 (公共成员函数) | |
| (C++17) |
将整数或浮点值转换为字符序列 (函数) |