std::basic_ostream<CharT,Traits>::operator=
来自 cppreference.cn
< 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) 复制赋值运算符是保护的,并且已被删除。输出流不是 CopyAssignable。
2) 移动赋值运算符交换基类的所有数据成员,除了 rdbuf(),与 rhs,如同通过调用 swap(*rhs)。此移动赋值运算符是受保护的:它仅由派生的可移动输出流类 std::basic_ofstream 和 std::basic_ostringstream 的移动赋值运算符调用,它们知道如何正确地移动赋值相关联的流缓冲区。
[编辑] 参数
rhs | - | 要赋值给 *this 的 basic_ostream 对象 |
[编辑] 示例
运行此代码
#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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 2067 | C++11 | 1. 重载 (1) 的参数类型为 basic_ostream& 2. 重载 (2) 的参数类型为 const basic_ostream&& |
1. 添加 const 2. 移除 const |