How to use the boost lexical_cast library for just for checking input

Posted by Inverse on Stack Overflow See other posts from Stack Overflow or by Inverse
Published on 2010-02-12T04:43:04Z Indexed on 2010/04/17 6:03 UTC
Read the original article Hit count: 197

Filed under:
|
|

I use the boost lexical_cast library for parsing text data into numeric values quite often. In several situations however, I only need to check if values are numeric; I don't actually need or use the conversion.

So, I was thinking about writing a simple function to test if a string is a double:

template<typename T> 
bool is_double(const T& s)
{
  try 
  {
    boost::lexical_cast<double>(s); 
    return true;
  }
  catch (...) 
  {
    return false;
  }
}

My question is, are there any optimizing compilers that would drop out the lexical_cast here since I never actually use the value?

Is there a better technique to use the lexical_cast library to perform input checking?

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost