byte shifting operators for iostream

in other languages, the << and >> operators are used for shifting bytes. in c++ it is also possible to use the two operators for byte shifting, but also for printing something on the console, for example: cout << "blah" << endl;. the byte shifting operators are in view a bit misused. in contrast to that, all common programming languages expose a function/method/procedure for that .. what were the reasons why a method is not used for that?
It was a questionable design decision to overload the << and >> operators for IO. The reason why STL does it is probably to make it easier to add I/O routines for your own types:

1
2
MyStruct foo;
cout << foo;  // can be done if you write your own << operator for MyStruct 


The above can't really be done with member functions because you can't add new member functions to iostream. You can, however, add as many << operators as you want.

Personally, I don't agree with the decision and feel that overloading the << and >> operators like this was a terrible idea.
Last edited on
thanks for you answer!
I don't personally mind it, because it's always fairly obvious when you mean to bit shift and when you just want to read or write to a stream. I feel that the way streams are set up in C++ is one of the languages strong points, because they're so versatile (in the sense that having the ostrteam istream classes as a base for all other streams allowing you to use the same operators for all other streams).
I have many complaints with the iostream library. The biggest one is that it's virtually impossible to extend. Have you ever tried to create your own iostream-derived type? I have... unsuccessfully. It's insanely hard and often implementation dependent. The kicker is that it really SHOULD be very simple. I mean all it comes down to is simple read/write data functions... and maybe an optional seek/tell. But nope! Not that easy.

My solution is to just not use it. Fortuantely, I/O in other libs (wx, SFML) is done a separate way -- probably for the same reason: iostream is lame.

That said... iostream isn't all bad. It definitely has some benefits over stdio (which is even less possible to extend). But stdio is also better in some ways. Neither are perfect... so pick your poison.
I've found that iostream is much easier to use than stdio, anyhow. How often do you even need to write your own stream?

Also, totally unrelated but do you prefer wx over Qt. Just out of curiosity :)
Last edited on
I never actually tried Qt so I can't say. I avoid widgetry unless it's absolutely necessary.
Disch wrote:
Have you ever tried to create your own iostream-derived type? I have... unsuccessfully

I had the need to do that, but boost.iostreams already did everything for me, including making it easy to create new filters and sinks/sources. http://www.boost.org/doc/libs/release/libs/iostreams/doc/index.html
Topic archived. No new replies allowed.