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.
op>>
so I accepted that one.
left-shift operator << is overloaded for stream output and is referred to as the stream insertion operator. right-shift operator >> is overloaded for stream input and is referred to as the stream extraction operator.
The extraction operator ( >> ), which is preprogrammed for all standard C++ data types, is the easiest way to get bytes from an input stream object. Formatted text input extraction operators depend on white space to separate incoming data values.
Input/Output Operators Overloading in C++ C++ is able to input and output the built-in data types using the stream extraction operator >> and the stream insertion operator <<. The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object.
The insertion and extraction operators are used to write information to or read information to or read information from, respectively, ostream and istream objects. By default, white space is skipped when the insertion and extraction operators are used.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With