命名空间
变体
操作

std::basic_ostream<CharT,Traits>::flush

来自 cppreference.cn
< cpp‎ | io‎ | basic_ostream
 
 
 
 
basic_ostream& flush();

将未提交的更改写入底层输出序列。行为类似于 UnformattedOutputFunction

如果 rdbuf() 是空指针,则不构造 sentry 对象。

否则,在构造并检查 sentry 对象后,调用 rdbuf()->pubsync()。如果调用返回 -1,则调用 setstate(badbit)

目录

[编辑] 返回值

*this

[编辑] 异常

如果 (exceptions() & badbit) != 0,则可能抛出 std::ios_base::failure

[编辑] 示例

#include <chrono>
#include <iostream>
#include <thread>
 
using namespace std::chrono_literals;
 
void f()
{
    std::cout << "Output from thread... ";
    for (int i{1}; i != 10; ++i)
    {
        std::this_thread::sleep_for(250ms);
        std::cout << i << ' ';
 
        // output three numbers at once;
        // the effect is observable only in real-time
        if (0 == (i % 3))
            std::cout.flush();
    }
    std::cout << std::endl; // flushes as well
}
 
int main()
{
    std::thread tr{f};
    std::this_thread::sleep_for(150ms);
    std::clog << "Output from main\n";
    tr.join();
}

输出

Output from main
Output from thread... 1 2 3 4 5 6 7 8 9

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 581 C++98 flush() 不行为类似 UnformattedOutputFunction
由于 LWG issue 60 的决议
行为类似
UnformattedOutputFunction(非格式化输出函数)

[编辑] 参阅

调用 sync()
(std::basic_streambuf<CharT,Traits> 的 public 成员函数) [编辑]
[virtual]
将缓冲区与关联的字符序列同步
(std::basic_streambuf<CharT,Traits> 的 virtual protected 成员函数) [编辑]
刷新输出流
(函数模板) [编辑]
输出 '\n' 并刷新输出流
(函数模板) [编辑]
与底层存储设备同步
(std::basic_istream<CharT,Traits> 的 public 成员函数) [编辑]