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

履歴 編集

function
<ostream>

std::basic_ostream::operator<<

// マニピュレータの実行
// 3つとも関数へのポインタを引数に取る。
basic_ostream&
  operator<<(basic_ostream& (*pf)(basic_ostream&));                       // (1) C++03
basic_ostream<CharT, Traits>&
  operator<<(basic_ios<CharT, Traits>& (*pf)(basic_ios<CharT, Traits>&)); // (2) C++03
basic_ostream<CharT, Traits>&
  operator<<(ios_base& (*pf)(ios_base&));                                 // (3) C++03

// bool値・数値・ポインタの書式化出力
basic_ostream& operator<<(bool n);                   // (4) C++03
basic_ostream& operator<<(short n);                  // (5) C++03
basic_ostream& operator<<(unsigned short n);         // (6) C++03
basic_ostream& operator<<(int n);                    // (7) C++03
basic_ostream& operator<<(unsigned int n);           // (8) C++03
basic_ostream& operator<<(long n);                   // (9) C++03
basic_ostream& operator<<(unsigned long n);          // (10) C++03
basic_ostream& operator<<(long long n);              // (11) C++11
basic_ostream& operator<<(unsigned long long n);     // (12) C++11
basic_ostream& operator<<(float f);                  // (13) C++03
basic_ostream& operator<<(double f);                 // (14) C++03
basic_ostream& operator<<(long double f);            // (15) C++03
basic_ostream& operator<<(extended-floating-point-type f); // (16) C++23
basic_ostream& operator<<(const void* p);            // (17) C++03
basic_ostream& operator<<(const volatile void* val); // (18) C++26
basic_ostream& operator<<(nullptr_t);                // (19) C++17

// ストリームバッファの非書式化出力
basic_ostream& operator<<(basic_streambuf<CharT, Traits>* sb); // (20) C++03

概要

ストリームへの出力またはマニピュレータの実行を行う。

  • (1)-(3) : マニピュレータを実行するオーバーロードそれ自体は、書式化出力関数・非書式化出力関数いずれにも該当しない
  • (4)-(19) : 数値型(boolも含む)とポインタに対するオーバーロードは、書式化出力関数である
  • (20) : basic_streambufに対するオーバーロードは、非書式化出力関数である

効果

(1)-(3) : マニピュレータの実行

  1. pf(*this)を呼び出す。

(4)-(18) : bool値・数値・ポインタの書式化出力

  1. sentryオブジェクトを構築する。sentryオブジェクトが失敗を示した場合、何もしない
  2. num_put::putを使用して入力のパース・数値への変換を行う。実引数を渡すに際し、一部の型では以下のように型変換を行う
    • (5) short:
      • flags()hexまたはoctが設定されていればstatic_cast<long>(static_cast<unsigned short>(n))
      • それ以外ではstatic_cast<long>(n)
    • (6) unsigned short: static_cast<unsigned long>(n)
    • (7) int:
      • flags()hexまたはoctが設定されていればstatic_cast<long>(static_cast<unsigned int>(n))
      • それ以外ではstatic_cast<long>(n)
    • (8) unsigned int: static_cast<unsigned long>(n)
    • (13) float: static_cast<double>(f)
    • (16) 拡張浮動小数点数型:
      • 変換順位がdouble以下であれば、static_cast<double>(f)
      • そうでなく変換順位がlong double以下であれば、static_cast<long double>(f)
      • そうでなければ実装定義の意味論を持ち、この演算子は条件付きサポートとなる
    • (18) const volatile void*:
      • return operator<<(const_cast<const void*>(val));
  3. num_put::putから得られたiostate値を実引数にしてsetstate関数を呼び出す

(19) : nullptr_tの出力

  • C++17 : 実装定義の出力文字列sを、return operator<<(s)として渡した場合と等価である。

(20) : ストリームバッファの非書式化出力

別のストリームバッファからの入力をストリームに出力する。

  1. sentryオブジェクトを構築する。sentryオブジェクトが失敗を示した場合、何もしない。
  2. 仮引数sbがヌルポインタの場合、setstate(badbit)を呼び出して終了する。
  3. 以下のいずれかを満たすまで、sbから文字を入力してthisへ出力する。
    • EOFに達した。
    • 出力処理に失敗した(この場合、失敗したときの文字は入力側のストリームバッファに戻される)。
    • 例外が発生した。

入力がなされなかった場合、setstate(failbit)を呼び出す。

戻り値

*this

備考

  • このクラスにはメンバ関数版のoperator<<と非メンバ関数版のoperator<<があるが、ロケールに依存して出力が変わる型へのオーバーロードが、メンバ関数版として定義される設計となっている。
  • (16) : 実装はCV修飾されていない全ての拡張浮動小数点数型に対するオーバーロードを提供する

#include <iostream>

int main() {
  std::cout << 101 << std::endl;
}

出力例

101

バージョン

言語

  • C++98
  • C++11: long longunsigned long longを実引数として受け取るものが追加された

関連項目

参照