title: Are there cases where the last operation succeeds but the istream/ostream goes fail()?
body: <p>I'm a new learner of C++. In my understanding, <code>fail()</code> indicates whether the <strong>last</strong> operation on the <code>istream</code>/<code>ostream</code> succeeded or not. For example, let <code>ifs</code> be an <code>ifstream</code> opened to some valid file:</p>
  <pre class="lang-cpp prettyprint-override"><code>string s;
  while (ifs &gt;&gt; s) {
      // 1. assume the reading was taken successfully
      //    and do something according to that
  }
  // 2. assume the reading was unsuccessful
  //    and do something according to that
  </code></pre>
  <p>May I ask if it is possible that, in some (corner) cases, we arrive at <code>2.</code> while the reading was actually successfully taken?</p>
  <p>If so, what we do at <code>2.</code> could be problematic. For example, if we have set <code>ifs.exceptions(ifs.exceptions() | ios_base::badbit);</code>, then we would (probably) have assumed that <code>ifs</code> is empty at <code>2.</code>.</p>
  <hr />
  <p>I came up with this question when I was self-learning C++ using Stroustrup's book <em>Programming: Principle and Practice Using C++</em> (2nd ed.). In Chapter 11.7 of this book, the writers design a <code>Punct_stream</code> class that takes an <code>istream</code> as <code>source</code>, processes data extracted from the <code>source</code> and then put the processed data into a data member of type <code>istringstream</code> called <code>buffer</code> waiting to be inputted into the program. The writers design the <code>operator bool()</code> method as follows (Page 404):</p>
  <pre class="lang-cpp prettyprint-override"><code>Punct_stream::operator bool()
  {
      return !(source.fail() || source.bad()) &amp;&amp; buffer.good();
  }
  </code></pre>
  <p>Firstly, I think <code>!(source.fail() || source.bad())</code> should be equivalent to just checking <code>source</code> since <code>fail()</code> accounts for both <code>failbit</code> and <code>badbit</code>, and checking <code>source</code> is equivalent to checking <code>!fail()</code>.</p>
  <p>What is more related to my question is that, by this design, <code>operator bool()</code> will first check if the <code>source</code> is in an error state, and if so, it won't even look at the <code>buffer</code>. But reading data from a <code>Punct_stream</code> into a <code>string</code> will be done as long as there is character in <code>buffer</code>. So, if the <code>buffer</code> is not empty and the <code>source</code> is <code>fail()</code> somehow, then reading from the <code>Punct_stream</code> into a <code>string</code> will actually succeed while the checking on the <code>Punct_stream</code> condition will be <code>false</code>.</p>
  <p>I wonder if the implementation of the standard library <code>istream</code>s may behave in some similar way such that in some cases the reading is actually successfully taken but the condition is evaluated to <code>false</code>?</p>