Namespaces
Variants
Views
Actions

std::basic_ostream::operator=

From cppreference.com
< cpp‎ | io‎ | basic ostream
Revision as of 20:41, 2 November 2012 by P12bot (Talk | contribs)

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

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

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

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

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

1) The copy assignment operator is protected, and is deleted. Output streams are not CopyAssignable.

2) The move assignment operator exchanges all data members of the base class, except for rdbuf(), with rhs, as if by calling swap(*rhs). This move assignment operator is protected: it is only called by the move assignment operators of the derived movable output stream classes std::basic_ofstream and std::basic_ostringstream, which know how to correctly move-assign the associated streambuffers.

Parameters

rhs - the basic_ostream object from which to assign to *this

Example

#include <sstream>
#include <utility>
#include <iostream>
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';
}

Output:

42