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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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.


iostream_withassign, ostream_withassign


istream_withassign



Member functions of iostream_withassign class:


iostream_withassign



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 class hierarchy





What problem are you trying to solve? The standard iostream classes do have mechanisms for changing the underlying buffer; what's wrong with using those?
– Kerrek SB
Nov 28 '15 at 14:04





@KerrekSB: then describe that mechanism? What are they?
– Destructor
Nov 28 '15 at 14:05





en.cppreference.com/w/cpp/io/basic_ios/rdbuf
– Kerrek SB
Nov 28 '15 at 14:06





@KerrekSB: but still not get an answer that what's wrong with _withassign classes?
– Destructor
Nov 28 '15 at 14:13




2 Answers
2



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


iostate rdstate()


void clear(iostate state = goodbit)


basic_streambuf<class charT, class Traits>* rdbuf()


basic_streambuf<class charT, class Traits>* rdbuf(basic_streambuf<class charT, class Traits>* sb)


basic_ios<class charT, class Traits>& copyfmt(basic_ios<class charT, class Traits>& rhs)





This answer could be improved by elaborating on in what way the old classes were deficient.
– Quuxplusone
Dec 25 '16 at 20:56



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.


private


delete



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 iostreams couldn't do.


*_withassign


iostream



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.


rdbuf


std::basic_streambuf


std::basic_streambuf


std::basic_filebuf



What you say can be easily achieved with:


std::ofstream ofspath;
auto oldbuf = cout.rdbuf( ofs.rdbuf() );






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard