命名空间
变体
操作

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) (since C++11)
basic_ostream& operator<<( unsigned long long value );
(5) (since 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) (since C++23)
basic_ostream& operator<<( std::nullptr_t );
(10) (since 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) (since 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)

        (std::basic_ostream<CharT, Traits>&) );
(20)

将数据插入到流中。

1-8) 插入 value
此函数行为如同 FormattedOutputFunction。在构造并检查 sentry 对象后,通过调用 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。在构造并检查 sentry 对象后,插入 longlval,如同 (2) 中那样,其中 lval
12) 插入来自 int value 的值。
此函数行为如同 FormattedOutputFunction。在构造并检查 sentry 对象后,插入 longlval,如同 (2) 中那样,其中 lval
13,14) 插入来自 unsigned shortunsigned int value 的值。
此函数行为如同 FormattedOutputFunction。在构造并检查 sentry 对象后,插入 static_cast<unsigned long>(value),如同 (3) 中那样。
15) 插入来自 float value 的值。
此函数行为如同 FormattedOutputFunction。在构造并检查 sentry 对象后,插入 static_cast<double>(value),如同 (6) 中那样。
16) 插入来自 value 的值。库为所有 cv-unqualified extended floating-point types 作为参数值的类型提供了重载。
此函数行为如同 FormattedOutputFunction。在构造并检查 sentry 对象后,检查 floating-point conversion rank of /* extended-floating-point-type */
  • 如果秩小于或等于 double 的秩,则插入 static_cast<double>(value),如同 (6) 中那样。
  • 否则,如果秩小于或等于 long double 的秩,则插入 static_cast<long double>(value),如同 (7) 中那样。
  • 否则,条件性地支持此重载的调用,并具有实现定义的语义。
17) 此函数行为如同 UnformattedOutputFunction。在构造并检查 sentry 对象后,检查 sb 是否为空指针。如果是,则执行 setstate(badbit) 并退出。否则,从 sb 控制的输入序列中提取字符,并将它们插入到 *this 中,直到满足以下条件之一
  • 输入序列上发生文件结束;
  • 在输出序列中插入失败(在这种情况下,不提取要插入的字符);
  • 发生异常(在这种情况下,捕获异常)。
如果没有插入任何字符,则执行 setstate(failbit)。如果在提取时抛出异常,则设置 failbit,并且如果 exceptions() 中设置了 failbit,则重新抛出异常。
18-20) 调用 func(*this)。这些重载用于实现输出 I/O 操纵符,例如 std::endl

目录

[编辑] 参数

value - 要插入的整数、浮点数、布尔值或指针值
func - 要调用的函数
sb - 指向从中读取数据的流缓冲区的指针

[编辑] 返回值

1-19) *this
20) func(*this)

[编辑] 注解

没有针对非静态成员的指针、volatile 指针、(直到 C++23) 或函数指针(签名被 (18-20) 重载接受的函数指针除外)的重载。

  • 尝试输出此类对象会调用隐式转换为 bool,并且对于任何非空指针值,都会打印值 1(除非设置了 boolalpha,在这种情况下会打印 true)。

字符和字符串参数(例如,类型为 charconst 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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 117 C++98 重载 (1-8,11-15) 将插入委托给
num_put::put,但它没有 short 的重载,
unsigned shortintunsigned intfloat
它们被转换
在传递之前
num_put::put
LWG 567 C++98 重载 (17) 的行为类似于 FormattedOutputFunction
由于 LWG issue 60 的决议
它的行为类似于
UnformattedOutputFunction

[编辑] 参见

插入字符数据或插入到右值流
(函数模板) [编辑]
对字符串执行流输入和输出
(函数模板) [编辑]
对字符串视图执行流输出
(函数模板) [编辑]
执行 bitset 的流输入和输出
(函数模板) [编辑]
序列化和反序列化复数
(函数模板) [编辑]
对伪随机数引擎执行流输入和输出
(函数模板) [编辑]
对伪随机数分布执行流输入和输出
(函数模板) [编辑]
插入一个字符
(公共成员函数) [编辑]
插入字符块
(公共成员函数) [编辑]
(C++17)
将整数或浮点值转换为字符序列
(函数) [编辑]