From 874ce50b8dfa74567a426c11bbaef7cda9ad0299 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Mon, 28 Jan 2019 11:07:49 +0000 Subject: Add overloads for ConvToNum to prevent (unsigned) char weirdness. --- include/convto.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 inline TOut ConvToNum(const std::string& in) return 0; return ret; } + +template<> inline char ConvToNum(const std::string& in) +{ + // We specialise ConvToNum for char to avoid istringstream treating + // the input as a character literal. + uint16_t num = ConvToNum(in); + return num <= UINT8_MAX ? num : 0; +} + +template<> inline unsigned char ConvToNum(const std::string& in) +{ + // We specialise ConvToNum for unsigned char to avoid istringstream + // treating the input as a character literal. + int16_t num = ConvToNum(in); + return num >= INT8_MIN && num <= INT8_MAX ? num : 0; +} -- cgit v1.2.3