简体   繁体   中英

Why were the iostream_withassign, ostream_withassign & istream_withassign classes removed from the C++ I/O system?

Prior to C++98, in the C++ I/O class hierarchy there were 3 classes named iostream_withassign, ostream_withassign & istream_withassign .

Member functions of iostream_withassign class:

Constructors & Destructor

~iostream_withassign

public:virtual ~iostream_withassign()

iostream_withassign

public:iostream_withassign()

Creates an iostream_withassign object. It does not do any initialization of this object.

operator =

public:iostream_withassign& operator =(iostream_withassign& rhs)

Assignment Operators operator =

Overload 1

public:iostream_withassign& operator =(streambuf*)

This assignment operator takes a pointer to a streambuf object and associates this streambuf object with the iostream_withassign object that is on the left side of the assignment operator.

Overload 2

public:iostream_withassign& operator =(ios&)

This assignment operator takes an lvalue reference to an ios object and associates the stream buffer attached to this ios object with the iostream_withassign object that is on the left side of the assignment operator.

Source: this .

Same way this says that:

The ostream_withassign class is a variant of ostream that allows object assignment. The predefined objects cout, cerr, and clog are objects of this class and thus may be reassigned at run time to a different ostream object. For example, a program that normally sends output to stdout could be temporarily directed to send its output to a disk file. It also contains constructor, destructor & =(assignment) operator functions.

I don't understand, why did these classes exist? Was there any use of these 3 classes? Why later on these 3 classes were removed from the C++98 standard? What is the reason?

See C++ stream class hierarchy also. It doesn't have these 3 classes.

C ++ I / O类层次结构

They where found to be deficient. They are replace by:

  1. iostate rdstate() to reads the stream state.
  2. void clear(iostate state = goodbit) to set the stream state.
  3. basic_streambuf<class charT, class Traits>* rdbuf() to retrieve the stream buffer.
  4. basic_streambuf<class charT, class Traits>* rdbuf(basic_streambuf<class charT, class Traits>* sb) to set the stream buffer.
  5. basic_ios<class charT, class Traits>& copyfmt(basic_ios<class charT, class Traits>& rhs) to set all other data members of rhs.

what's wrong with _withassign classes?

Consistency. Since the C++98 standard I/O streams were thought to not be copied, initially by making the relevant operations private (until C++11) and then by explicitly delete -ing them.

The way you handle a resource matters in C++ because you can decide to exclusively own it or share it, unlike in other languages where you cannot and need to get used to the one the language chose. Having both versions ruins (and it's an euphemism) consistency. It's counter-intuitive.

Strictly speaking, moreover, there isn't much the *_withassign wrapper would add that iostream s couldn't do.


For example, a program that normally sends output to stdout could be temporarily directed to send its output to a disk file. It also contains constructor, destructor & =(assignment) operator functions.

You can use rdbuf to get the underlying std::basic_streambuf the stream's currently using and provide another one, obtained by another standard stream or one you wrote by inheriting from the std::basic_streambuf class like std::basic_filebuf does, for example.

What you say can be easily achieved with:

std::ofstream ofs{path};
auto oldbuf = cout.rdbuf( ofs.rdbuf() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM