• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <ostream>

    std::basic_ostream::flush

    basic_ostream<CharT, Traits>& flush();
    

    概要

    (非書式化出力関数)ストリームバッファをフラッシュする。

    効果

    1. rdbuf() がヌルポインタであれば、何もしない。
    2. sentry オブジェクトを構築する。sentry オブジェクトが失敗を示した場合、何もしない。
    3. rdbuf()->pubsync() を呼び出す。
    4. pubsync()-1 を返した場合は、setstate(badbit) を呼び出す。(それによって、例外 ios_base::failure が送出されるかもしれない)
    5. 上記処理中に例外が送出された場合、出力ストリームの現在の状態に ios_base::badbit を設定し、(exceptions() & ios_base::badbit) != 0 の場合、当該例外を再送出する。

    戻り値

    *this

    備考

    本関数は、直接呼ぶのではなく、同名のマニピュレータ flush、あるいは改行を伴うマニピュレータ endl から間接的に呼び出されるのが一般的である。

    #include <iostream>
    
    int main() {
      std::cout << "cpprefjp";
      std::cout.flush();
      std::cout << "cpprefjp";
    }
    

    出力

    cpprefjpcpprefjp
    

    実装例

    template <class CharT, class Traits>
    basic_ostream<CharT, Traits>& basic_ostream<CharT, Traits>::flush()
    {
      if (this->rdbuf()) {
        try {
          sentry s(*this);
          if (s) {
            if (this->rdbuf()->pubsync() == pos_type(-1)) {
              this->setstate(ios_base::badbit);
            }
          }
        } catch (...) {
          // ここで、本ストリームの状態に ios_base::badbit を設定する(例外は投げない)
          if ((this->exceptions() & ios_base::badbit) != 0) {
            throw;
          }
        }
      }
      return *this;
    }
    

    バージョン

    言語

    • C++98

    参照