1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-09-20 05:25:37 +01:00

optimised conversions from string to numerical types

This commit is contained in:
Antonin Portelli 2014-02-12 18:36:36 +00:00
parent 9484934a8a
commit 4d25722736
2 changed files with 28 additions and 2 deletions

View File

@ -26,3 +26,21 @@ using namespace Latan;
const string Env::fullName = PACKAGE_STRING; const string Env::fullName = PACKAGE_STRING;
const string Env::name = PACKAGE_NAME; const string Env::name = PACKAGE_NAME;
const string Env::version = PACKAGE_VERSION; const string Env::version = PACKAGE_VERSION;
template <>
float Latan::strTo<float>(const string &str)
{
return strtof(str.c_str(), (char **)NULL);
}
template <>
double Latan::strTo<double>(const string &str)
{
return strtod(str.c_str(), (char **)NULL);
}
template <>
int Latan::strTo<int>(const string &str)
{
return (int)(strtol(str.c_str(), (char **)NULL, 10));
}

View File

@ -49,7 +49,7 @@ namespace Env
// string conversions // string conversions
template <typename T> template <typename T>
T strTo(std::string str) T strTo(const std::string &str)
{ {
T buf; T buf;
std::istringstream stream(str); std::istringstream stream(str);
@ -59,8 +59,16 @@ T strTo(std::string str)
return buf; return buf;
} }
//// optimized specializations
template <>
float strTo<float>(const std::string &str);
template <>
double strTo<double>(const std::string &str);
template <>
int strTo<int>(const std::string &str);
template <typename T> template <typename T>
std::string strFrom(T x) std::string strFrom(const T x)
{ {
std::ostringstream stream; std::ostringstream stream;