This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
re: Another problem with istringstream
- To: libstdc++ at sourceware dot cygnus dot com
- Subject: re: Another problem with istringstream
- From: Benjamin Kosnik <bkoz at cygnus dot com>
- Date: Tue, 11 Jan 2000 00:38:51 -0800
#include <iostream>
#include <string>
#include <sstream>
int main()
{
string s = "123";
istringstream is(s);
int i, j;
if (is >> i >> j)
cout << "ok" << endl;
}
> outputs "ok" but actually it shouldn't, because ">> j" fails and j
> gets garbage.
if (is >> i >> j)
Is true. This is not a bug. Here's why:
istream::operator>>int
istream::operator>>int
basic_ios::operator void*()
This last call checks, as per the standard:
operator void*() const
{ return this->fail() ? 0 : const_cast<basic_ios*>(this); }
Translation: it's only looking for failbit or badbit. Neither are set
(eofbit, is, but that isn't being checked. . .) so yes, "ok" gets printed.
Try checking for is.good()
-benjamin