Add lexical casts for float
This commit is contained in:
parent
15c46090d5
commit
0621eb4f0e
2 changed files with 62 additions and 0 deletions
50
src/util.cpp
50
src/util.cpp
|
@ -249,3 +249,53 @@ double lexical_cast_default<double, const char*>(const char* a, double def)
|
|||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
float lexical_cast<float, const std::string&>(const std::string& a)
|
||||
{
|
||||
char* endptr;
|
||||
float res = strtof(a.c_str(), &endptr);
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
float lexical_cast<float, const char*>(const char* a)
|
||||
{
|
||||
char* endptr;
|
||||
float res = strtof(a, &endptr);
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
template<>
|
||||
float lexical_cast_default<float, const std::string&>(const std::string& a, float def)
|
||||
{
|
||||
char* endptr;
|
||||
float res = strtof(a.c_str(), &endptr);
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
return def;;
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
float lexical_cast_default<float, const char*>(const char* a, float def)
|
||||
{
|
||||
char* endptr;
|
||||
float res = strtof(a, &endptr);
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
return def;
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
12
src/util.hpp
12
src/util.hpp
|
@ -141,6 +141,18 @@ double lexical_cast_default<double, const std::string&>(const std::string& a, do
|
|||
template<>
|
||||
double lexical_cast_default<double, const char*>(const char* a, double def);
|
||||
|
||||
template<>
|
||||
float lexical_cast<float, const std::string&>(const std::string& a);
|
||||
|
||||
template<>
|
||||
float lexical_cast<float, const char*>(const char* a);
|
||||
|
||||
template<>
|
||||
float lexical_cast_default<float, const std::string&>(const std::string& a, float def);
|
||||
|
||||
template<>
|
||||
float lexical_cast_default<float, const char*>(const char* a, float def);
|
||||
|
||||
template<typename From>
|
||||
std::string str_cast(From a)
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue