std::basic_ostream<CharT,Traits>:: operator=
From cppreference.net
<
cpp
|
io
|
basic ostream
|
protected
:
basic_ostream & operator = ( const basic_ostream & rhs ) = delete ; |
(1) | |
|
protected
:
basic_ostream & operator = ( basic_ostream && rhs ) ; |
(2) | (C++11以降) |
1)
コピー代入演算子はprotectedで、削除されています。出力ストリームは
CopyAssignable
ではありません。
2)
ムーブ代入演算子は、基底クラスのすべてのデータメンバーを、
rdbuf()
を除いて、
rhs
と交換します。これは、
swap
(
*
rhs
)
を呼び出すかのように行われます。このムーブ代入演算子はprotectedです:これは、派生したムーブ可能な出力ストリームクラスである
std::basic_ofstream
および
std::basic_ostringstream
のムーブ代入演算子によってのみ呼び出されます。これらのクラスは、関連付けられたストリームバッファを正しくムーブ代入する方法を知っています。
パラメータ
| rhs | - |
割り当て元の
basic_ostream
オブジェクト
*
this
へ
|
例
このコードを実行
#include <iostream> #include <sstream> #include <utility> int main() { std::ostringstream s; // std::cout = s; // ERROR: copy assignment operator is deleted // std::cout = std::move(s); // ERROR: move assignment operator is protected s = std::move(std::ostringstream() << 42); // OK, moved through derived std::cout << s.str() << '\n'; }
出力:
42
不具合報告
以下の動作変更に関する欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2067 | C++11 |
1. オーバーロード
(1)
のパラメータ型は
basic_ostream&
2. オーバーロード (2) のパラメータ型は const basic_ostream && |
1.
const
を追加
2. const を削除 |