Namespaces
Variants

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

From cppreference.net
basic_ostream & flush ( ) ;

未コミットの変更を基になる出力シーケンスに書き込みます。 UnformattedOutputFunction として振る舞います。

rdbuf() が null ポインタの場合、セントリオブジェクトは構築されません。

それ以外の場合、セントリーオブジェクトの構築とチェックを行った後、 rdbuf ( ) - > pubsync ( ) を呼び出します。この呼び出しが - 1 を返した場合、 setstate ( badbit ) を呼び出します。

目次

戻り値

* this

例外

std::ios_base::failure がスローされる可能性があります( ( exceptions ( ) & badbit ) ! = 0 の場合)。

#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 << ' ';
        // 一度に3つの数値を出力;
        // この効果はリアルタイムでのみ観察可能
        if (0 == (i % 3))
            std::cout.flush();
    }
    std::cout << std::endl; // こちらもフラッシュを行う
}
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++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
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メンバ関数)