Hi,
your article about setting the stream state correctly when encountering invalid data was very interesting. I know now, how to handle an invalid state from the perspective of the operator implementation, but when i'm using the input operator, how can i display an error message, which points the user in the right direction? How should the caller of the operator deal with this situation?
I only played a little bit with input operators and it seemed more natural to me, to throw an exception with a detailed description of what was going on. My implementation of your example would be more like this:
template< class char_type, class char_traits>
std::basic_istream< char_type, char_traits> & operator >> (
std::basic_istream< char_type, char_traits> & streamIn, complex & value)
{
int nReal, nImag;
char_type ch;
streamIn >> ch;
if ( ch != '(')
throw ComplexBadFormatException("Missing opening bracket.");
streamIn >> nReal;
streamIn >> ch;
if ( ch != ',')
throw ComplexBadFormatException("Missing colon.");
streamIn >> nImag;
streamIn >> ch;
if ( ch != ')')
throw ComplexBadFormatException("Missing closing bracket.");
value.m_nReal = nReal;
value.m_nImag = nImag;
return streamIn;
}
greetings, Volker.