Converting string to integer, double, float without having to catch exceptions

c++, exception

Solution

To avoid exceptions, go back to a time when exceptions didn't exist. These functions were carried over from C but they're still useful today: `strtod` and `strtol`. (There's also a `strtof` but doubles will auto-convert to float anyway). You check for errors by seeing if the decoding reached the end of the string, as indicated by a zero character value.

char * pEnd = NULL;
double d = strtod(str.c_str(), &pEnd);
if (*pEnd) // error was detected

Problem

I have a `string` which can be either a `double`, `float` or `int`. I would like to convert the `string` to the data type by making function calls. I am currently using functions such as `stof` and `stoi` which throw exceptions when the input is not a `float` or `int`. Is there another way to convert the strings without having to catch exceptions? Perhaps some function that passes a a pointer to a `float` as argument and just returns a `boolean` which represents the success of the function of call. I would like to avoid using any `try` `catch` statements in any of my code.

Original source

Related problems