名稱空間
變體
操作

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

來自 cppreference.com
< 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 成員函式) [編輯]