开发者

C++ Streams compared to .NET Streams?

Is there any reason the standard library still uses the streams that it do开发者_运维技巧es? It seems that .net streams are much easier to use and implement. I can understand them keeping them due to compatibility reasons but why haven't they added a new stream implementation more like .net?

What are some pros and cons of the C++ streams compared to .NET streams? I Can't come up with any pros for C++ streams :.

Like nos has stated

"He's talking about the concept not the MS implementation. Substitute e.g. .NET for Java and the question would mean pretty much the same (as its concepts of streams is similar to that of .NET but unlike C++)"


While they both adhere to the basic abstraction, C++ and .NET interpret 'stream' in different senses. With this in mind, it's not really fair to compare .NET's Stream to C++'s *stream because they serve different purposes: one is meant for moving bytes into and out of a store, and the other is meant to help with data representation. Both frameworks do have analogs in the other, however:

  • .NET: Stream : *Reader
  • C++: streambuf : iostream

Regarding the C++ side, check out this quote from the GNU C++ Iostream docs, which says it better than I can:

The istream and ostream classes are meant to handle conversion between objects in your program and their textual representation.

By contrast, the underlying streambuf class is for transferring raw bytes between your program, and input sources or output sinks.

I think you'll find when you make the appropriate comparison that things make more sense. (Member lists for Stream versus streambuf are very similar, as are TextReader and istream.)


C++ streams are an old feature of C++, and changing them now would be prohibitive. This doesn't mean that they can't be improved, but they won't be replaced. Nobody anywhere close to the C++ Standard Committee is going to suggest adding a parallel stream implementation. That would cause a great deal of confusion for limited benefits.

That being said, what's so good about .NET streams? I'm a C++ guy with limited exposure to .NET. I agree that C++ streams can be clunky in spots, but they generally get the job done without too much hassle. What about .NET streams would justify significant changes to C++?


I don't know about .Net, but since you say the question would be the same for Java streams, the most obvious pro- for C++ streams is that they have all the formatting built into std::ios. You can quite reasonably argue that this is orthogonal to the business of shunting bytes around, and therefore should be a separate concern. Then again C++ does separate the two, (see streambuf) it's just that the thing it happens to call a stream is the thing that also does the formatting, whereas Java calls the simple thing a stream, and then the programmer adorns it with with scanners, or readers, or whatnot.

As usual, Java provides a simple interface, C++ provides a "highly configurable and immensely powerful" interface which is kind of fiddly. Ease of use is not the only concern of the C++ standard committee.

As for why the committee hasn't provided a more Java-like alternative API, as Microsoft has done in .NET, I suspect:

  • The committee prefers to let third parties do that kind of adaptor work.
  • The committee likes many aspects of the current API, for example the all-purpose overloading of operator<< and operator>>. I'm not sure whether this is part of what you dislike about C++ streams, but it's certainly part of what people who do like them, like about them.

Anyway there hasn't actually been a significant update of the C++ standard since it was first published, so even if the committee wanted a change (which it doesn't AFAIK), it would by now still only be in draft form, for release maybe this year, maybe next year.


C++ streams were badly designed from the beginning. Too bad so many people have written code using it and it is impossible to take it out from the standard. It is difficult to extend (trying adding compression or encryption features to streams is a nightmare).

Having used C++ streams for years, I no longer use it in any new projects. Google Coding Guidelines agrees with me. It says "Use streams only for logging".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜