std::basic_ostream<CharT,Traits>::flush
From 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++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 581 | C++98 | flush() は UnformattedOutputFunction として動作しませんでした。それは |
〜として動作します。 UnformattedOutputFunction |
[編集] 関連項目
| sync() を呼び出す ( std::basic_streambuf<CharT,Traits> の public メンバ関数) | |
| [virtual] |
バッファを関連付けられた文字シーケンスと同期する ( std::basic_streambuf<CharT,Traits> の仮想 protected メンバ関数) |
| 出力ストリームをフラッシュする (関数テンプレート) | |
| '\n'を出力し、出力ストリームをフラッシュする (関数テンプレート) | |
| 基礎となるストレージデバイスと同期する ( std::basic_istream<CharT,Traits> の public メンバ関数) |