A
aaragon
Hi everyone,
I know that to obtain a double from a string, you can use something
like the following (from the C++-FAQ):
template <typename T>
T stream_cast(std::string& s) {
T x;
static std::stringstream iss;
iss.clear();
iss.str(s);
iss>>x;
if (!iss) {
std::string err("*** ERROR *** ");
err += s + " cannot be converted to expected type with type id:\n";
err += typeid(x).name();
throw std::runtime_error(err);
}
return x;
}
However, there has to be a faster way to accomplish this, and to fall
back to this implementation if things don't go as expected. Is what
I'm thinking possible? Could I use a try/catch block on the <cstdlib>
function stof()?
Thank you all,
aa
I know that to obtain a double from a string, you can use something
like the following (from the C++-FAQ):
template <typename T>
T stream_cast(std::string& s) {
T x;
static std::stringstream iss;
iss.clear();
iss.str(s);
iss>>x;
if (!iss) {
std::string err("*** ERROR *** ");
err += s + " cannot be converted to expected type with type id:\n";
err += typeid(x).name();
throw std::runtime_error(err);
}
return x;
}
However, there has to be a faster way to accomplish this, and to fall
back to this implementation if things don't go as expected. Is what
I'm thinking possible? Could I use a try/catch block on the <cstdlib>
function stof()?
Thank you all,
aa