std::basic_ostream<CharT,Traits>:: basic_ostream
From cppreference.net
<
cpp
|
io
|
basic ostream
|
explicit
basic_ostream
(
std::
basic_streambuf
<
CharT, Traits
>
*
sb
)
;
|
(1) | |
|
protected
:
basic_ostream ( const basic_ostream & rhs ) = delete ; |
(2) | (C++11 起) |
|
protected
:
basic_ostream ( basic_ostream && rhs ) ; |
(3) | (C++11 起) |
2)
复制构造函数受保护,且已被删除。输出流不可复制。
3)
移动构造函数使用
basic_ios
<
CharT, Traits
>
::
move
(
rhs
)
来移动所有
basic_ios
成员(除
rdbuf()
外)从
rhs
到
*
this
。该移动构造函数受保护:它由可移动输出流类
std::basic_ofstream
和
std::basic_ostringstream
的移动构造函数调用,这些类知道如何正确移动关联的流缓冲区。
参数
| sb | - | 用作输出序列的流缓冲区 |
| rhs | - | 用于初始化的 basic_ostream 对象 |
注释
由于
basic_ios::init(sb)
在
sb
为空指针时会设置
badbit
,且由于
basic_ostream::sentry
在流已处于失败状态时不会执行任何操作,因此向由空指针
sb
构造的流执行写入操作是无实际效果的。
示例
运行此代码
#include <iostream> #include <sstream> #include <utility> int main() { // 错误:拷贝构造函数已被删除 // std::ostream myout(std::cout); // 正确:与 cout 共享缓冲区 std::ostream myout(std::cout.rdbuf()); // 错误:移动构造函数受保护 // std::ostream s2(std::move(std::ostringstream() << 7.1)); // 正确:通过派生类调用移动构造函数 std::ostringstream s2(std::ostringstream() << 7.1); myout << s2.str() << '\n'; std::ostream dev_null{nullptr}; // 参见上文说明 dev_null << "no-op"; }
输出:
7.1