title: Can operator>> read an int hex AND decimal?
body: <p>Can I persuade <code>operator&gt;&gt;</code> in C++ to read both a <code>hex</code> value AND and a <code>decimal</code> value?  The following program demonstrates how reading hex goes wrong.  I'd like the same <code>istringstream</code> to be able to read both <code>hex</code> and <code>decimal</code>.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  
  int main(int argc, char** argv)
  {
      int result = 0;
      // std::istringstream is(&quot;5&quot;); // this works
      std::istringstream is(&quot;0x5&quot;); // this fails
  
      while ( is.good() ) {
          if ( is.peek() != EOF )
              is &gt;&gt; result;
          else
              break;
      }
  
      if ( is.fail() )
          std::cout &lt;&lt; &quot;failed to read string&quot; &lt;&lt; std::endl;
      else
          std::cout &lt;&lt; &quot;successfully read string&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;result: &quot; &lt;&lt; result &lt;&lt; std::endl;
  }
  </code></pre>