您的解决方案似乎很好。但是,如果您想要更好的编译时优化,您应该考虑进行模板特化。在提供的代码中,您根据类型进行分支。然而,这可能会被编译到代码中,而不是在编译时解析(已经有可能)。此外,如果您想根据类型添加额外的检查,该函数很快就会变得混乱。
我编写了我自己的转换代码版本,除了你的代码之外,它还检查是否为整数类型提供了科学记数法:
#include <type_traits>
#include <utility>
#include <string>
#include <limits>
#include <algorithm>
template <typename T>
auto to_T(const std::string &s) -> std::enable_if_t<std::is_floating_point<T>::value, std::pair<bool, T>>
{
return std::pair<bool, T>{true, T(std::stold(s))}; //read the string into the biggest floating point possible, and do a narrowing conversion
}
template <typename T>
auto to_T(const std::string &s) -> std::enable_if_t<!std::is_floating_point<T>::value && std::is_signed<T>::value, std::pair<bool, T>>
{
return ((long long)(std::numeric_limits<T>::min()) <= std::stoll(s) && //does the integer in the string fit into the types data range?
std::stoll(s) <= (long long)(std::numeric_limits<T>::max()))
? std::pair<bool, T>{true, T(std::stoll(s))}
: std::pair<bool, T>{false, 0}; //if yes, read the string into the biggest possible integer, and do a narrowing conversion
}
template <typename T>
auto to_T(const std::string &s) -> std::enable_if_t<!std::is_floating_point<T>::value && std::is_unsigned<T>::value, std::pair<bool, T>>
{
return ((unsigned long long)(std::numeric_limits<T>::min()) <= std::stoull(s) && //does the integer in the string fit into the types data range?
std::stoull(s) <= (unsigned long long)(std::numeric_limits<T>::max()))
? std::pair<bool, T>{true, T(std::stoull(s))}
: std::pair<bool, T>{false, 0}; //if yes, read the string into the biggest possible integer, and do a narrowing conversion
}
template <typename T>
auto decode(const std::string &s) -> std::enable_if_t<std::is_floating_point<T>::value, std::pair<bool, T>>
{
return s.empty() ? //is the string empty?
std::pair<bool, T>{false, 0}
: to_T<T>(s); //if not, convert the string to a floating point number
}
template <typename T>
auto decode(const std::string &s) -> std::enable_if_t<!std::is_floating_point<T>::value && std::is_signed<T>::value, std::pair<bool, T>>
{
return (s.empty() || //is the string empty?
std::find(std::begin(s), std::end(s), '.') != std::end(s) || //or does it not fit the integer format?
std::find(std::begin(s), std::end(s), ',') != std::end(s) ||
std::find(std::begin(s), std::end(s), 'e') != std::end(s) ||
std::find(std::begin(s), std::end(s), 'E') != std::end(s))
? std::pair<bool, T>{false, 0}
: to_T<T>(s); //if not, convert the string to a signed integer value
}
template <typename T>
auto decode(const std::string &s) -> std::enable_if_t<!std::is_floating_point<T>::value && std::is_unsigned<T>::value, std::pair<bool, T>>
{
return (s.empty() || //is the string empty?
std::find(std::begin(s), std::end(s), '.') != std::end(s) || //or does it not fit the integer format?
std::find(std::begin(s), std::end(s), ',') != std::end(s) ||
std::find(std::begin(s), std::end(s), 'e') != std::end(s) ||
std::find(std::begin(s), std::end(s), 'E') != std::end(s) ||
std::find(std::begin(s), std::end(s), '-') != std::end(s))
? //or does it have a sign?
std::pair<bool, T>{false, 0}
: to_T<T>(s); //if not, convert the string to an unsigned integer value
}
这仍然需要在平台之间进行一些移植,因为std::stold、std::stoll 或std::stoull 可能不可用。但除此之外,它应该独立于平台类型的实现。
编辑:
我忘记了 decode 不应该读取数字,而是返回 0 的情况。现在已修复。