Boost logo

Boost :

From: Gary Powell (Gary.Powell_at_[hidden])
Date: 2001-05-18 11:24:02


Regarding operator>>(istream, octonion),
  where the statement,

  is >> varName;

  if (is.fail() ) goto finish;

I think should be replaced by

  is >> varName;

  if (!is.good() ) goto finish;

My copy of Standard C++ IOStreams and Locales p.35 bottom of the page.
(section 1.3.2) has a recommendation that we check for error situations via
fail(), and failure of input operations via !good(). This is because good()
takes all flags into account, the eofbit included, wheras fail() ignores the
eofbit.

Since is >> varName appears to me to be an input operation, the check should
be !good().

Also should the state of the stream be checked after the first call to
is.putback(ch); before its called the second time? My reading of the
standard says putback can fail.

WARNING I haven't used this and therefore may be totally off the wall here.
I was wondering if a input steam iterator should be used to avoid having to
call putback(). (On the theory that you can't putback to a readonly file)

Another suggestion is to use peek() in the places where no matter what the
next character is, you do a putback. There is at least one place where this
would work. The comment on the if is "if ( cc == '(' ) // read "(((", where
both for the if and the else the next statement is "is.putback(ch);"

and "if (cc == '(' ) // read "((a),(("
it looks like the second call to putback() here is required.

While I didn't see a requirement in the standard, my "C++ Programming
Language" section 21.6.2 has a comment to the effect that "what is
guaranteed is that you can backup one character after a successful read."
I'm not sure how that effects, a read, then a peek though.

Again if there is someone who understands this better than I, please review
the above suggestions. (Talk about dark corners of C++!)

  -gary-

gary.powell_at_[hidden]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk