what does stringstream do on failure?

Started by
2 comments, last by Zahlman 18 years, 7 months ago
What will happen if I run code like this:

std::stringstream s;
int i;
s << "HELLO";
s >> i;
Does it throw an exception? It would also be helpful if somebody could point me to the section in the C++ standard which specifies this behaviour.
Advertisement
Quote:Original post by Nitage
Does it throw an exception?


By default, it sets the fail bit on the stream. You can request it to throw an exception instead with std::basic_ios::exceptions()

Quote:It would also be helpful if somebody could point me to the section in the C++ standard which specifies this behaviour.


Stream classes are addressed in sections 27.4 to 27.8.
String streams are addressed in section 27.7.

std::basic_ios is in section 27.4.4.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
basic_istringstream, basic_ostringstream and basic_stringstream are all descendants of ios_base. In this regard it should return a 'badbit' if the stream fails completely, otherwise it would return a 'failbit' if the stream is generally OK but the operation was not processed correctly. My view is that what you've shown would result in a failbit.

These can be used to throw an exeception, but I don't think that an exception is thrown automatically. This is what I can glean from Josuttis, 'The C++ Standard Library'.

--random_thinker
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
It does not "return" anything, but instead those state bits are set within the stream object. Thus 'i' is not changed by the operation (and is thus still an uninitialized value), and further attempts to read from the stream will similarly fail, unless/until you .clear() the stream (and probably also .ignore() the data that caused the problem). Just like any other stream.

Keep in mind that if you're constructing the stringstream from data that in turn came from some other stream, and you want to throw away all that data on failure (say you're reading a line at a time from stdin, and want to skip lines until the user finally gets it right ;) ), you could just construct an entirely separate stringstream from the next bit of data:

int berateUserUntilIGetAnInt() {  int result;  cout << "Give me an int!" << endl;  while (true) {    string s;    getline(cin, s);    stringstream ss(s);    if (ss >> result) return result;    // Otherwise, got some bad input. Berate user and try again.    // The stringstream gets its dtor called here, and next time through    // it will be constructed again from the next string of input.    // Er, I think that works... don't have access to a compiler ATM ^^;    cout << "No, I said an int, you moron!" << endl;  }}

This topic is closed to new replies.

Advertisement