Sign in to follow this  
Nitage

what does stringstream do on failure?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this