operator<<(std::basic_ostream)
ja.cppreference.net より
| ヘッダ <ostream>
|
||
basic_ostream で定義 |
||
template< class CharT, class Traits >
basic_ostream<CharT, Traits>&
operator<<( basic_ostream<CharT, Traits>& os, CharT ch );
template< class CharT, class Traits >
basic_ostream<CharT, Traits>&
operator<<( basic_ostream<CharT, Traits>& os, char ch );
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, char ch );
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, signed char ch );
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, unsigned char ch );
|
(1) | |
basic_ostream (2) |
||
template< class CharT, class Traits >
basic_ostream<CharT, Traits>&
operator<<( basic_ostream<CharT, Traits>& os, const CharT* s );
template< class CharT, class Traits >
basic_ostream<CharT, Traits>&
operator<<( basic_ostream<CharT, Traits>& os, const char* s );
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, const char* s );
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, const signed char* s );
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, const unsigned char* s );
|
(3) | |
basic_ostream (C++11以上) |
||
template< class Ostream, class T >
Ostream&& operator<<( Ostream&& os, const T& value );
|
(C++11以上) | (C++11以上) |
| (C++11以上) basic_ostream (C++11以上) |
||
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, wchar_t ch ) = delete;
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, char8_t ch ) = delete;
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, char16_t ch ) = delete;
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, char32_t ch ) = delete;
template< class Traits >
basic_ostream<wchar_t, Traits>&
operator<<( basic_ostream<wchar_t, Traits>& os, char8_t ch ) = delete;
template< class Traits >
basic_ostream<wchar_t, Traits>&
operator<<( basic_ostream<wchar_t, Traits>& os, char16_t ch ) = delete;
template< class Traits >
basic_ostream<wchar_t, Traits>&
operator<<( basic_ostream<wchar_t, Traits>& os, char32_t ch ) = delete;
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, const wchar_t* s ) = delete;
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, const char8_t* s ) = delete;
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, const char16_t* s ) = delete;
template< class Traits >
basic_ostream<char, Traits>&
operator<<( basic_ostream<char, Traits>& os, const char32_t* s ) = delete;
template< class Traits >
basic_ostream<wchar_t, Traits>&
operator<<( basic_ostream<wchar_t, Traits>& os, const char8_t* s ) = delete;
template< class Traits >
basic_ostream<wchar_t, Traits>&
operator<<( basic_ostream<wchar_t, Traits>& os, const char16_t* s ) = delete;
template< class Traits >
basic_ostream<wchar_t, Traits>&
operator<<( basic_ostream<wchar_t, Traits>& os, const char32_t* s ) = delete;
|
(4) | (C++20以上) |
文字または文字列を挿入します。
1) FormattedOutputFunction として動作します。sentry オブジェクトを構築して確認した後、文字
ch を挿入します。ch の型が char であり、文字コンテナ型が os でない場合、代わりに char が挿入されます。os.widen(ch) パディングは以下のように決定されます:
- もし
os.width() > 1ならば、出力文字にos.width() - 1がos.fill()コピー追加され、出力文字シーケンスを形成します。 - もし
(out.flags() & std::ios_base::adjustfield) == std::ios_base::leftならば、フィル文字は出力文字の後に配置され、そうでなければ前に配置されます。
挿入後、
os.width(0) が呼び出されて、std::setw の効果があればそれを打ち消します。2) FormattedOutputFunction として動作します。sentry オブジェクトを構築して確認した後、最初の要素が
s によって指し示される文字配列から、連続する文字を挿入します。
- 一つ目と三つ目のオーバーロード (
CharTがchの型と一致する場合) では、正確にtraits::length(s)文字が挿入されます。 - 二つ目のオーバーロードでは、正確に
std::char_traits<char>::length(s)文字が挿入されます。 - 最後の二つのオーバーロードでは、正確に
traits::length(reinterpret_cast<const char*>(s))が挿入されます。
挿入の前に、最初にすべての文字が
os.widen() を使用して拡張され、次にパディングが以下のように決定されます:
- 挿入する文字数が
os.width()より少ない場合、文字シーケンスの長さがos.fill()と等しくなるように、十分な数のos.width()コピーが追加されます。 - もし
(out.flags() & std::ios_base::adjustfield) == std::ios_base::leftならば、フィル文字は出力シーケンスの末尾に追加され、そうでなければ出力シーケンスの前に追加されます。
挿入後、
os.width(0) が呼び出されて、std::setw の効果があればそれを打ち消します。もし
s がヌルポインタであれば、動作は未定義です。3) 出力ストリームオブジェクトへの右辺値参照が与えられ、適切な挿入演算子を呼び出します (
os << value と同等)。このオーバーロードは、式 os << value が well-formed であり、かつ Ostream が std::ios_base から public かつ曖昧でない派生をしたクラス型である場合にのみ、オーバーロード解決に参加します。4)
char16_t、char32_t など (またはそれらのヌル終端シーケンス) を受け入れるオーバーロードは削除されています: std::cout << u'X' は許可されません。以前は、これらは整数値またはポインタ値を出力していました。パラメータ
| os | - | データを挿入する出力ストリーム |
| ch | - | 挿入する文字への参照 |
| s | - | 挿入する文字列へのポインタ |
戻り値
1,2)
os
3)
std
::
move
(
os
)
注記
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>
)
|