diff options
author | Peter Powell <petpow@saberuk.com> | 2019-01-28 11:07:49 +0000 |
---|---|---|
committer | Peter Powell <petpow@saberuk.com> | 2019-01-28 11:08:19 +0000 |
commit | 874ce50b8dfa74567a426c11bbaef7cda9ad0299 (patch) | |
tree | 8e3912062a7de50786fbfc7c596d75ad4c2f0dae | |
parent | fa6cfec3f8befcc650214b37664569e52f21e6b8 (diff) |
Add overloads for ConvToNum to prevent (unsigned) char weirdness.
-rw-r--r-- | include/convto.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/include/convto.h b/include/convto.h index 3332580ed..2f808d1fb 100644 --- a/include/convto.h +++ b/include/convto.h @@ -99,3 +99,19 @@ template<typename TOut> inline TOut ConvToNum(const std::string& in) return 0; return ret; } + +template<> inline char ConvToNum<char>(const std::string& in) +{ + // We specialise ConvToNum for char to avoid istringstream treating + // the input as a character literal. + uint16_t num = ConvToNum<uint16_t>(in); + return num <= UINT8_MAX ? num : 0; +} + +template<> inline unsigned char ConvToNum<unsigned char>(const std::string& in) +{ + // We specialise ConvToNum for unsigned char to avoid istringstream + // treating the input as a character literal. + int16_t num = ConvToNum<int16_t>(in); + return num >= INT8_MIN && num <= INT8_MAX ? num : 0; +} |