Does anyone actually use stream extraction operators?

Dan picture Dan · Feb 22, 2010 · Viewed 7.4k times · Source

I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful.

I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for std::string). Are these appropriate only for short example programs and textbooks? Is operator>> a failed feature of C++?

Questions have been asked about safely overloading stream operators. What I wonder is if anyone does this in practice.

Even for something simple like reading input from a file in C++ I can't suggest using operator>>. It's too difficult to write code that is robust in detecting and handling errors in input (or I don't know how).

If you disagree please show a good example of using operator>> -- maybe by answering that last question I linked to.


Wrapup: Thanks for the responses everyone, lots of good opinions. Manuel's answer made me reconsider my reluctance to using op>> so I accepted that one.

Answer

Manuel picture Manuel · Feb 22, 2010

I think stream extractor operators can be very useful when combined with STL algorithms like std::copy and with the std::istream_iterator class.

Read this answer to see what I'm talking about.