title: C++ stringstream and fail vs eof
body: <p>Wondering what's going on here. I've tried to push this through gdb but can't seem to get a debug version / code of libstdc++, so hoping someone who knows their streams can help? In the code below, the key bit is the while loop:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;iomanip&gt;
  #include &lt;sstream&gt;
  using namespace std;
  void printer(int i) {
          cout &lt;&lt; i &lt;&lt; ", ";
  }
  int main ()
  {
          string s;
          getline(cin, s);
          stringstream input(s);
          vector&lt;int&gt; v1;
          int i;
          while (!input.fail())
          {
                  input &gt;&gt; i;
                  v1.push_back(i);
          } 
          for_each(v1.begin(), v1.end(), printer);
          return 0;
  }
  </code></pre>
  
  <p>When I run this and supply a number, this appears to add that number twice to the vector. If I change the while loop's condition to <code>eof()</code> (as opposed to <code>fail()</code>) everything behaves as I'd have expected.</p>
  
  <p>So given the extraction <code>operator&gt;&gt;</code> is supposed to extract values from a stream, how is it possible that successive calls keep producing the result?</p>
  
  <p>I'm guessing that I'm not thinking about this quite right (it's 2:30am localtime so that might have something to do with it!)</p>
  
  <p>Using gcc version 4.9.2 (Ubuntu 4.9.2-0ubuntu1~12.04)</p>