Is there a rise in lexical_cast equivalent to C # TryParse?

Introduction (from Eric Lippert's blog)

Exceptions are the result of unsuccessful design decisions. Exceptional exceptions are thrown into completely non-exceptional ones and therefore it must be caught and processed all the time.

A classic example of an unpleasant exception is Int32.Parse, which throws if you give it a string that cannot be parsed as an integer. But a 99% use case for this method converts the strings entered by the user, which can be any old, and therefore it is in no way exceptional for parsing. Worse, call for it to determine if their argument is ahead of the curve badly without implementing the whole method, in which case they would not need to be called first.

Now the important part:

This unsuccessful design decision was so annoying that, of course, Soon after that, the development team conducted TryParse, correctly.

From MSDN Int32.TryParse:

Return Value Type: System.Boolean true if s was converted successfully; otherwise false.

So, a colleague was still working on a small amount of code that required checking whether the string is a number, so thinking about it and realizing that there is no good solution in C ++ (basically this is for__each / find_if or boost: lexical_cast try catch) I thought it would be nice to have is_convertible or something from boost?

Ofc I can wrap boost lexical_cast and return true at the end of the try block and return false at the end of the catch block, but I prefer existing practice solutions :).

+4
c ++ c # boost exception
Nov 28 '12 at 10:19
source share
3 answers

If you can use boost, you can use boost::conversion::try_lexical_convert :

 #include <boost/lexical_cast/try_lexical_convert.hpp> std::string str("1.2"); double res; if(boost::conversion::try_lexical_convert<double>(str, res)){ //everything normal } else{ //we got a problem } 
+1
Feb 23 '17 at 15:02
source share
— -

> So, a colleague recenly worked on a small amount of code that required checking whether a string is a number, therefore, thinking about it and realizing that there is no good solution in C ++

In C ++ 11, you have std::stol and / or std::stod that can do what you need.

Update If you do not want to use exceptions, then strtol(str, &endp) will perform the conversion.

You can check if str == endp after the call; if they are the same, then no conversion was possible (since endp points to the beginning of the uncured part of the line)

Like this:

 strtol(str, &endp); if (endp==str) { /* no conversion occurred */ } 
+3
Nov 28 '12 at 17:05
source share

In fact, to be honest, as far as I know, there is no try_lexical_cast , but you can do two things.

Own usage streams and verifying that the extraction was successful, and not that in most cases lexical_cast uses the streams inside anyway:

  std::string str="56.7"; std::istringstream ss(str); double d; if(ss >> d) { //passed } else //failed 

Or of course, as you mentioned, you can wrap lexical_cast

0
Nov 28 '12 at 10:33
source share



All Articles