title: How can I extend a lexical cast to support enumerated types?
body: <p>I have the following function that will convert a string into a numeric data type:</p>
  
  <pre><code>template &lt;typename T&gt;
  bool ConvertString(const std::string&amp; theString, T&amp; theResult)
  {
      std::istringstream iss(theString);
      return !(iss &gt;&gt; theResult).fail();
  }
  </code></pre>
  
  <p>This does not work for enumerated types, however, so I have done something like this:</p>
  
  <pre><code>template &lt;typename T&gt;
  bool ConvertStringToEnum(const std::string&amp; theString, T&amp; theResult)
  {
      std::istringstream iss(theString);
      unsigned int temp;
      const bool isValid = !(iss &gt;&gt; temp).fail();
      theResult = static_cast&lt;T&gt;(temp);
      return isValid;
  }
  </code></pre>
  
  <p>(I'm making the assumption that theString has a valid value for the enumerated type; I'm using this mainly for simple serialization)</p>
  
  <p>Is there a way to create a single function that combines both of these?  </p>
  
  <p>I've played a bit with the template arguments but haven't come up with anything; it'd just be nice not to have to call one function for enumerated types and another for everything else.</p>
  
  <p>Thank you</p>