From adce0245dc493bfbbb2621646f91694397eeb060 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 17 Aug 2014 07:53:55 -0700 Subject: [PATCH] Fix a warning. --- format.cc | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/format.cc b/format.cc index f206c5ae..f488bcf7 100644 --- a/format.cc +++ b/format.cc @@ -115,6 +115,25 @@ struct IsLongDouble { enum {VALUE = 0}; }; template <> struct IsLongDouble { enum {VALUE = 1}; }; +// Checks if a value fits in int - used to avoid warnings about comparing +// signed and unsigned integers. +template +struct IntChecker { + template + static bool fits_in_int(T value) { + unsigned max = INT_MAX; + return value <= max; + } +}; + +template <> +struct IntChecker { + template + static bool fits_in_int(T value) { + return value >= INT_MIN && value <= INT_MAX; + } +}; + const char RESET_COLOR[] = "\x1b[0m"; typedef void (*FormatFunc)(fmt::Writer &, int , fmt::StringRef); @@ -207,7 +226,7 @@ class PrecisionHandler : template int visit_any_int(T value) { - if (value < INT_MIN || value > INT_MAX) + if (!IntChecker::is_signed>::fits_in_int(value)) throw fmt::FormatError("number is too big in format"); return static_cast(value); }