std::basic_ostream<CharT,Traits>::basic_ostream
来自 cppreference.cn
< 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() { // ERROR: copy ctor is deleted // std::ostream myout(std::cout); // OK: shares buffer with cout std::ostream myout(std::cout.rdbuf()); // ERROR: move constructor is protected // std::ostream s2(std::move(std::ostringstream() << 7.1)); // OK: move ctor called through the derived class std::ostringstream s2(std::ostringstream() << 7.1); myout << s2.str() << '\n'; std::ostream dev_null{nullptr}; // see Notes above dev_null << "no-op"; }
输出
7.1