简体   繁体   English

为什么从C ++ I / O系统中删除iostream_withassign,ostream_withassign和istream_withassign类?

[英]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 . 在C ++ 98之前,在C ++ I / O类层次结构中,存在3个名为iostream_withassign, ostream_withassignistream_withassign

Member functions of iostream_withassign class: iostream_withassign类的成员函数:

Constructors & Destructor 构造函数与析构函数

~iostream_withassign 〜iostream_withassign

public:virtual ~iostream_withassign()

iostream_withassign iostream_withassign

public:iostream_withassign()

Creates an iostream_withassign object. 创建一个iostream_withassign对象。 It does not do any initialization of this object. 它不会对此对象进行任何初始化。

operator = 运算子=

public:iostream_withassign& operator =(iostream_withassign& rhs)

Assignment Operators operator = 分配运算符operator =

Overload 1 过载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. 该分配运算符获取指向streambuf对象的指针,并将该streambuf对象与该赋值运算符左侧的iostream_withassign对象相关联。

Overload 2 过载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. 该赋值运算符采用对ios对象的左值引用,并将附加到此ios对象的流缓冲区与在赋值运算符左侧的iostream_withassign对象相关联。

Source: this . 资料来源: this

Same way this says that: 原路说:

The ostream_withassign class is a variant of ostream that allows object assignment. ostream_withassign类是ostream的一种变体,它允许对象分配。 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. 预定义的对象cout,cerr和clog是此类的对象,因此可以在运行时重新分配给其他ostream对象。 For example, a program that normally sends output to stdout could be temporarily directed to send its output to a disk file. 例如,可以将通常将输出发送到stdout的程序临时定向,以将其输出发送到磁盘文件。 It also contains constructor, destructor & =(assignment) operator functions. 它还包含构造函数,析构函数和=(assignment)运算符函数。

I don't understand, why did these classes exist? 我不明白,为什么这些类存在? Was there any use of these 3 classes? 这3个类有什么用吗? Why later on these 3 classes were removed from the C++98 standard? 为什么稍后将这3个类从C ++ 98标准中删除? What is the reason? 是什么原因?

See C++ stream class hierarchy also. 另请参见C ++流类层次结构。 It doesn't have these 3 classes. 它没有这3个类。

C ++ I / O类层次结构

They where found to be deficient. 他们被发现不足。 They are replace by: 它们被替换为:

  1. iostate rdstate() to reads the stream state. iostate rdstate()读取流状态。
  2. void clear(iostate state = goodbit) to set the stream state. void clear(iostate state = goodbit)设置流状态。
  3. basic_streambuf<class charT, class Traits>* rdbuf() to retrieve the stream buffer. basic_streambuf<class charT, class Traits>* rdbuf()以检索流缓冲区。
  4. basic_streambuf<class charT, class Traits>* rdbuf(basic_streambuf<class charT, class Traits>* sb) to set the stream buffer. basic_streambuf<class charT, class Traits>* rdbuf(basic_streambuf<class charT, class Traits>* sb)设置流缓冲区。
  5. basic_ios<class charT, class Traits>& copyfmt(basic_ios<class charT, class Traits>& rhs) to set all other data members of rhs. basic_ios<class charT, class Traits>& copyfmt(basic_ios<class charT, class Traits>& rhs)设置basic_ios<class charT, class Traits>& copyfmt(basic_ios<class charT, class Traits>& rhs)所有其他数据成员。

what's wrong with _withassign classes? _withassign类有什么问题?

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. 由于认为不复制C ++ 98标准I / O流,因此首先将相关操作设为private (直到C ++ 11),然后再显式delete -ing它们。

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. 在C ++中,处理资源的方式很重要,因为您可以决定专有或共享它,而在其他语言中,您不能并且需要习惯于所选择的一种语言。 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. 而且,严格来说, *_withassign包装器并不会增加iostream无法做到的东西。


For example, a program that normally sends output to stdout could be temporarily directed to send its output to a disk file. 例如,可以将通常将输出发送到stdout的程序临时定向,以将其输出发送到磁盘文件。 It also contains constructor, destructor & =(assignment) operator functions. 它还包含构造函数,析构函数和=(assignment)运算符函数。

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 ofs{path};
auto oldbuf = cout.rdbuf( ofs.rdbuf() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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