diff --git a/misc.h b/misc.h index 4e385d24..f3cfce7b 100644 --- a/misc.h +++ b/misc.h @@ -6,6 +6,8 @@ #include "stdcpp.h" #include "trap.h" +#include + #ifdef _MSC_VER #if _MSC_VER >= 1400 // VC2005 workaround: disable declarations that conflict with winnt.h @@ -71,6 +73,16 @@ struct CompileAssert static char dummy[2*b-1]; }; +// Checks signed input to be negative or positive. +template < typename T, bool sig> struct negative{ + bool operator()(const T &x){ return x < 0; } +}; + +// Unsigned input won't be checked, this avoids the compiler warning when unsigned int (or long unsigned int) is used. +template < typename T> struct negative{ + bool operator()(const T &x){ return false; } +}; + // __attribute__ ((unused)) will help silence the "unused variable warnings. Its available // at least as early as GCC 2.9.3 (https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC84) // This also works into our -Wall -Wextra strategy for warnings. @@ -532,8 +544,7 @@ std::string IntToString(T a, unsigned int base = 10) if (a == 0) return "0"; bool negate = false; - if (a < 0) - { + if (negative::is_signed>()(a)) { negate = true; a = 0-a; // VC .NET does not like -a }