title: Ways std::stringstream can set fail/bad bit?
body: <p>A common piece of code I use for simple string splitting looks like this:</p>
  
  <pre><code>inline std::vector&lt;std::string&gt; split(const std::string &amp;s, char delim) {
      std::vector&lt;std::string&gt; elems;
      std::stringstream ss(s);
      std::string item;
      while(std::getline(ss, item, delim)) {
          elems.push_back(item);
      }
      return elems;
  }
  </code></pre>
  
  <p>Someone mentioned that this will silently "swallow" errors occurring in <code>std::getline</code>. And of course I agree that's the case. But it occurred to me, what could possibly go wrong here <strong>in practice</strong> that I would need to worry about. basically it all boils down to this:</p>
  
  <pre><code>inline std::vector&lt;std::string&gt; split(const std::string &amp;s, char delim) {
      std::vector&lt;std::string&gt; elems;
      std::stringstream ss(s);
      std::string item;
      while(std::getline(ss, item, delim)) {
          elems.push_back(item);
      }
  
      if(/* what error can I catch here? */) {
          // *** How did we get here!? ***
      }
  
      return elems;
  }
  </code></pre>
  
  <p>A <code>stringstream</code> is backed by a <code>string</code>, so we don't have to worry about any of the issues associated with reading from a file. There is no type conversion going on here since <code>getline</code> simply reads until it sees the line delimeter or <code>EOF</code>. So we can't get any of the errors that something like <code>boost::lexical_cast</code> has to worry about.</p>
  
  <p>I simply can't think of something besides failing to allocate enough memory that could go wrong, but that'll just throw a <code>std::bad_alloc</code> well before the <code>std::getline</code> even takes place. What am I missing?</p>