Namespaces
Variants
Views
Actions

std::basic_ostream::basic_ostream

From cppreference.com
< cpp‎ | io‎ | basic ostream
Revision as of 09:34, 15 February 2013 by Cubbi (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl ">

<td >
explicit basic_ostream( std::basic_streambuf<CharT, Traits>* sb );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
protected:
basic_ostream( const basic_ostream& rhs ) = delete;
</td>

<td > (2) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
protected:
basic_ostream( basic_ostream&& rhs );
</td>

<td > (3) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

1) Constructs the basic_ostream object, assigning initial values to the base class by calling basic_ios::init(sb).

2) The copy constructor is protected, and is deleted. Output streams are not copyable.

3) The move constructor uses basic_ios<CharT, Traits>::move(rhs) to move all basic_ios members, except for the rdbuf(), from rhs into *this. This move constructor is protected: it is called by the move constructors of movable output stream classes std::basic_ofstream and std::basic_ostringstream, which know how to correctly move the associated streambuffer.

Parameters

sb - streambuffer to use as output sequence
rhs - basic_ostream to initialize from

Example

#include <sstream>
#include <utility>
#include <iostream>
int main()
{
//  std::ostream myout(std::cout);              // ERROR: copy ctor is deleted
    std::ostream myout(std::cout.rdbuf());      // OK: shares buffer with cout
//  std::ostream s2(std::move(std::ostringstream() << 7.1));       // ERROR: move constructor
                                                                   // is protected
    std::ostringstream s2(std::move(std::ostringstream() << 7.1)); // OK: move ctor called
                                                                   // through the derived class
    myout << s2.str() << '\n';
}

Output:

7.1