最終更新日時:
が更新

履歴 編集

function
<locale>

std::num_put::do_put

protected:
  virtual iter_type do_put(iter_type out, ios_base& str, char_type fill, bool val) const;               // (1) C++98
  virtual iter_type do_put(iter_type out, ios_base& str, char_type fill, long val) const;               // (2) C++98
  virtual iter_type do_put(iter_type out, ios_base& str, char_type fill, long long val) const;          // (3) C++11
  virtual iter_type do_put(iter_type out, ios_base& str, char_type fill, unsigned long val) const;      // (4) C++98
  virtual iter_type do_put(iter_type out, ios_base& str, char_type fill, unsigned long long val) const; // (5) C++11
  virtual iter_type do_put(iter_type out, ios_base& str, char_type fill, double val) const;             // (6) C++98
  virtual iter_type do_put(iter_type out, ios_base& str, char_type fill, long double val) const;        // (7) C++98
  virtual iter_type do_put(iter_type out, ios_base& str, char_type fill, const void* val) const;        // (8) C++98

概要

数値・真偽値・ポインタを書式化して出力する。put()から呼び出される、実際の書式化を行う仮想関数である。

  • (1) : boolを出力する
  • (2), (3) : 符号付き整数型 (long, long long) を出力する
  • (4), (5) : 符号なし整数型 (unsigned long, unsigned long long) を出力する
  • (6), (7) : 浮動小数点数型 (double, long double) を出力する
  • (8) : ポインタ (const void*) を出力する

効果

(2)〜(8)は、valを書式化して文字列outへ書き込む。以下の説明でloclocale loc = str.getloc();で初期化されるローカル変数である。処理は以下の4段階(Stage 1〜4)で行われる。

Stage 1 : 変換指定の決定

str.flags()からbasefielduppercasefloatfieldshowposshowbaseshowpointを取り出し、printfの変換指定を決定する。各表は上から順に評価され、最初に真となった行が適用される。

さらに、型に応じて長さ修飾子が付加される。

長さ修飾子
long l
long long ll
unsigned long l
unsigned long long ll
long double L
それ以外 なし

また、以下の修飾子が変換指定の前に付加される。

状態 stdioの変換指定に相当
整数型 showpos +
整数型 showbase #
浮動小数点数型 showpos +
浮動小数点数型 showpoint #

浮動小数点数型からの変換では、floatfield != (fixed | scientific)である場合にstr.precision()が精度として指定される。そうでない場合、精度は指定されない。

Stage 1の終了時点での表現は、上記で決定した変換指定sを用いてprintf(s, val)を呼び出した場合に出力されるcharの列である(現在のロケールが"C"ロケールであると仮定する)。

Stage 2 : 文字の変換と桁区切りの挿入

小数点'.'以外の各文字cは、use_facet<ctype<charT>>(loc).widen(c)によってcharTへ変換される。

算術型については、numpunct::do_grouping()が返す値に従って、numpunct::thousands_sep()の文字が列に挿入される。

小数点'.'numpunct::decimal_point()に置き換えられる。

Stage 3 : 埋め文字の位置の決定

adjustfield = (flags &std::ios_base::adjustfield)として、埋め文字の位置は以下のように決まる。

状態 位置
adjustfield ==std::ios_base::left 後ろに詰める
adjustfield ==std::ios_base::right 前に詰める
adjustfield ==std::ios_base::internalで、表現に符号が現れる場合 符号の後ろに詰める
adjustfield ==std::ios_base::internalで、Stage 1の表現が0xもしくは0Xで始まる場合 xまたはXの後ろに詰める
それ以外 前に詰める

str.width()が非0で、Stage 2の後の列のcharTの数がstr.width()より少ない場合、列の長さがstr.width()になるまで、上記の位置にfillが追加される。その後、str.width(0)が呼び出される。

Stage 4 : 出力

Stage 3の終了時点でのcharTの列が、*out++ = cによって出力される。

(1) bool版の処理

戻り値

  • (1)〜(8) : out

バージョン

言語

  • C++98

関連項目