diff --git a/format.h b/format.h index 0dc987f4..57c56491 100644 --- a/format.h +++ b/format.h @@ -220,39 +220,34 @@ struct TypeSelector { typedef uint32_t Type; }; template <> struct TypeSelector { typedef uint64_t Type; }; +// Checks if a number is negative - used to avoid warnings. +template +struct SignChecker { + template + static bool IsNegative(T value) { return false; } +}; + +template <> +struct SignChecker { + template + static bool IsNegative(T value) { return value < 0; } +}; + +// Returns true if value is negative, false otherwise. +// Same as (value < 0) but doesn't produce warnings if T is an unsigned type. template -struct IntTraitsBase { +inline bool IsNegative(T value) { + return SignChecker::is_signed>::IsNegative(value); +} + +template +struct IntTraits { // Smallest of uint32_t and uint64_t that is large enough to represent // all values of T. typedef typename TypeSelector::digits <= 32>::Type MainType; }; -// Information about an integer type. -template -struct IntTraits : IntTraitsBase { - typedef T UnsignedType; - static bool IsNegative(T) { return false; } -}; - -template -struct SignedIntTraits : IntTraitsBase { - typedef UnsignedT UnsignedType; - static bool IsNegative(T value) { return value < 0; } -}; - -#define FMT_INT_TRAIT(Type) \ - template <> \ - struct IntTraits : SignedIntTraits {}; - -FMT_INT_TRAIT(char) -FMT_INT_TRAIT(short) -FMT_INT_TRAIT(int) -FMT_INT_TRAIT(long) - -template <> -struct IntTraits : SignedIntTraits {}; - template struct IsLongDouble { enum {VALUE = 0}; }; @@ -867,7 +862,7 @@ void BasicWriter::FormatInt(T value, const Spec &spec) { char sign = 0; typedef typename internal::IntTraits::MainType UnsignedType; UnsignedType abs_value = value; - if (internal::IntTraits::IsNegative(value)) { + if (value < 0) { sign = '-'; ++size; abs_value = 0 - abs_value; @@ -877,8 +872,7 @@ void BasicWriter::FormatInt(T value, const Spec &spec) { } switch (spec.type()) { case 0: case 'd': { - typename internal::IntTraits::MainType normalized_value = abs_value; - unsigned num_digits = internal::CountDigits(normalized_value); + unsigned num_digits = internal::CountDigits(abs_value); CharPtr p = PrepareFilledBuffer(size + num_digits, spec, sign) + 1 - num_digits; internal::FormatDecimal(GetBase(p), abs_value, num_digits); @@ -1409,7 +1403,7 @@ class FormatInt { template inline void FormatDec(char *&buffer, T value) { typename internal::IntTraits::MainType abs_value = value; - if (internal::IntTraits::IsNegative(value)) { + if (internal::IsNegative(value)) { *buffer++ = '-'; abs_value = 0 - abs_value; }