operator<< (std::basic_ostream)
|
ヘッダで定義
<ostream>
|
||
|
basic_ostream と文字
|
||
| (1) | ||
|
template
<
class
CharT,
class
Traits
>
basic_ostream
<
CharT, Traits
>
&
|
||
|
template
<
class
CharT,
class
Traits
>
basic_ostream
<
CharT, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
basic_ostreamと文字配列
|
||
| (2) | ||
|
template
<
class
CharT,
class
Traits
>
basic_ostream
<
CharT, Traits
>
&
|
||
|
template
<
class
CharT,
class
Traits
>
basic_ostream
<
CharT, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
basic_ostream 右辺値
|
||
|
template
<
class
Ostream,
class
T
>
Ostream && operator << ( Ostream && os, const T & value ) ; |
(3) | (C++11以降) |
|
basic_ostream および UTF 文字/配列に対する削除されたオーバーロード
|
||
| (4) | (C++20以降) | |
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
wchar_t
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
wchar_t
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
wchar_t
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
char
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
wchar_t
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
wchar_t
, Traits
>
&
|
||
|
template
<
class
Traits
>
basic_ostream
<
wchar_t
, Traits
>
&
|
||
文字または文字列を挿入します。
- もし os. width ( ) > 1 の場合、 os. width ( ) - 1 個の os. fill ( ) のコピーが出力文字に追加され、出力文字シーケンスを形成します。
- もし ( out. flags ( ) & std:: ios_base :: adjustfield ) == std:: ios_base :: left の場合、フィル文字は出力文字の後に配置され、それ以外の場合は前に配置されます。
-
第1および第3のオーバーロード(
CharTが ch の型と一致する場合)では、正確に traits :: length ( s ) 文字が挿入される。 - 第2のオーバーロードでは、正確に std:: char_traits < char > :: length ( s ) 文字が挿入される。
- 最後の2つのオーバーロードでは、正確に traits :: length ( reinterpret_cast < const char * > ( s ) ) 文字が挿入される。
- 挿入する文字数が os. width ( ) より少ない場合、 os. fill ( ) のコピーを文字シーケンスに追加して、その長さを os. width ( ) と等しくします。
- もし ( out. flags ( ) & std:: ios_base :: adjustfield ) == std:: ios_base :: left の場合、フィル文字は出力シーケンスの末尾に追加されます。それ以外の場合は出力シーケンスの前に追加されます。
Ostream
が
std::ios_base
から公開かつ明確に派生したクラス型である場合にのみ、オーバーロード解決に参加する。
目次 |
パラメータ
| os | - | データを挿入する出力ストリーム |
| ch | - | 挿入する文字への参照 |
| s | - | 挿入する文字列へのポインタ |
戻り値
注記
LWG issue 1203 以前は、 LWG issue 1203 より前では、以下のようなコード ( std:: ostringstream ( ) << 1.2 ) . str ( ) はコンパイルできませんでした。
例
#include <fstream> #include <iostream> void foo() { // エラー: operator<< (basic_ostream<char, _Traits>&, char8_t) は削除されています // std::cout << u8'z' << '\n'; } std::ostream& operator<<(std::ostream& os, char8_t const& ch) { return os << static_cast<char>(ch); } int main() { std::cout << "Hello, world" // `const char*` オーバーロードを使用 << '\n'; // `char` オーバーロードを使用 std::ofstream{"test.txt"} << 1.2; // rvalue オーバーロードを使用 std::cout << u8'!' << '\n'; // プログラム定義の operator<<(os, char8_t const&) を使用 }
出力:
Hello, world !
不具合報告
以下の動作変更に関する欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 167 | C++98 |
すべてのオーバーロードで挿入される文字数が
(2) において traits :: length ( s ) であった |
CharT
の型が
ch
の型と一致しないオーバーロードの
文字数を更新 |
| LWG 1203 | C++11 |
右辺値ストリームのオーバーロードが
基底クラスの左辺値参照を返した |
派生クラスの右辺値参照を
返す |
| LWG 2011 | C++98 | パディングが std::num_put::do_put() によって決定された |
演算子自体によって
決定される |
| LWG 2534 | C++11 |
右辺値ストリームのオーバーロードが
制約されていなかった |
制約されている |
関連項目
|
書式化されたデータを挿入
(public member function) |
|
|
(C++23)
|
引数の
書式化
された表現を出力
(function template) |
|
文字をワイド文字に変換
(public member function of
std::basic_ios<CharT,Traits>
)
|