Vla*_*eev 7 c++ templates string-conversion
目标是一个函数,给定包含数字的字符串和整数类型T,如果值适合没有溢出的类型,则返回成功+转换值,否则返回失败.
使用std::istringstream读取从字符串作品某些情况下,一个数字:
template<typename T>
std::pair<bool, T> decode(std::string s)
{
T value;
std::istringstream iss(s);
iss >> std::dec >> value;
return std::pair<bool, T>(!iss.fail(), value);
}
template<typename T>
void testDecode(std::string s)
{
std::pair<bool, T> result = decode<T>(s);
if (result.first)
std::cout << +result.second;
else
std::cout << "ERROR";
std::cout << std::endl;
}
int main()
{
testDecode<int32_t>("12"); // 12
testDecode<int16_t>("1000000"); // ERROR
testDecode<int16_t>("65535"); // ERROR
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,对于8位类型(因为它们被视为chars)失败了:
testDecode<uint8_t>("12"); // 49 !
Run Code Online (Sandbox Code Playgroud)
负数也被错误地接受并解析为无符号类型:
testDecode<uint16_t>("-42"); // 65494 !
Run Code Online (Sandbox Code Playgroud)
这种功能由例如std.conv.toD和str::parseRust提供.什么是C++等价物?
你的解决方案看起来不错。但是,如果您想要更好的编译时优化,您应该考虑进行模板专门化。在所提供的代码中,您可以根据类型进行分支。然而,这可能会被编译到代码中,而不是在编译时解决(这已经是可能的)。此外,如果您想根据类型添加额外的检查,该函数很快就会变得混乱。
我编写了我自己版本的转换代码,除了您的转换代码之外,它还检查整数类型是否给出了科学记数法:
#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
}
Run Code Online (Sandbox Code Playgroud)
这仍然需要在平台之间进行某种程度的移植,因为std::stold、std::stoll或std::stoull可能不可用。但除此之外,它应该独立于平台类型实现。
编辑:
decode我忘记了一个不应该读取数字的情况,而是返回0了。现在这个问题已经解决了。
| 归档时间: |
|
| 查看次数: |
183 次 |
| 最近记录: |