diff --git a/format.h b/format.h index 9a1fa111..0948bd50 100644 --- a/format.h +++ b/format.h @@ -124,14 +124,12 @@ struct IntTraits {}; template struct SignedIntTraits { - typedef T Type; typedef UnsignedT UnsignedType; static bool IsNegative(T value) { return value < 0; } }; template struct UnsignedIntTraits { - typedef T Type; typedef T UnsignedType; static bool IsNegative(T) { return false; } }; @@ -143,10 +141,10 @@ template <> struct IntTraits : UnsignedIntTraits {}; template <> -struct IntTraits : SignedIntTraits {}; +struct IntTraits : SignedIntTraits {}; template <> -struct IntTraits : UnsignedIntTraits {}; +struct IntTraits : UnsignedIntTraits {}; class ArgInserter; class FormatterProxy; @@ -288,42 +286,38 @@ class IntFormatter : public SpecT { T value() const { return value_; } }; -// Returns an integer formatter that formats value in the octal base. -// internal::IntTraits::Type is used instead of T to avoid instantiating -// the function for types smaller than int similarly to enable_if. -template -inline IntFormatter< - typename internal::IntTraits::Type, TypeSpec<'o'> > oct(T value) { - return IntFormatter >(value, TypeSpec<'o'>()); +#define DEFINE_INT_FORMATTERS(TYPE) \ +/* Returns an integer formatter that formats value in the octal base. */ \ +inline IntFormatter > oct(TYPE value) { \ + return IntFormatter >(value, TypeSpec<'o'>()); \ +} \ + \ +inline IntFormatter > hex(TYPE value) { \ + return IntFormatter >(value, TypeSpec<'x'>()); \ +} \ + \ +inline IntFormatter > hexu(TYPE value) { \ + return IntFormatter >(value, TypeSpec<'X'>()); \ +} \ + \ +template \ +inline IntFormatter > pad( \ + IntFormatter > f, \ + unsigned width, char fill = ' ') { \ + return IntFormatter >( \ + f.value(), AlignTypeSpec(width, fill)); \ +} \ + \ +inline IntFormatter > pad( \ + TYPE value, unsigned width, char fill = ' ') { \ + return IntFormatter >( \ + value, AlignTypeSpec<0>(width, fill)); \ } -template -inline IntFormatter< - typename internal::IntTraits::Type, TypeSpec<'x'> > hex(T value) { - return IntFormatter >(value, TypeSpec<'x'>()); -} - -template -inline IntFormatter< - typename internal::IntTraits::Type, TypeSpec<'X'> > hexu(T value) { - return IntFormatter >(value, TypeSpec<'X'>()); -} - -template -inline IntFormatter< - typename internal::IntTraits::Type, AlignTypeSpec > pad( - IntFormatter > f, unsigned width, char fill = ' ') { - return IntFormatter >( - f.value(), AlignTypeSpec(width, fill)); -} - -template -inline IntFormatter< - typename internal::IntTraits::Type, AlignTypeSpec<0> > pad( - T value, unsigned width, char fill = ' ') { - return IntFormatter >( - value, AlignTypeSpec<0>(width, fill)); -} +DEFINE_INT_FORMATTERS(int) +DEFINE_INT_FORMATTERS(long) +DEFINE_INT_FORMATTERS(unsigned) +DEFINE_INT_FORMATTERS(unsigned long) class BasicFormatter { private: @@ -570,8 +564,8 @@ class Formatter : public BasicFormatter { int int_value; unsigned uint_value; double double_value; - int64_t long_value; - uint64_t ulong_value; + long long_value; + unsigned long ulong_value; long double long_double_value; const void *pointer_value; struct { diff --git a/format_test.cc b/format_test.cc index 0ead31f9..bfcf2db3 100644 --- a/format_test.cc +++ b/format_test.cc @@ -1065,10 +1065,11 @@ TEST(TempFormatterTest, Examples) { } TEST(StrTest, oct) { + EXPECT_EQ("12", (BasicFormatter() << oct(static_cast(012))).str()); EXPECT_EQ("12", (BasicFormatter() << oct(012)).str()); - EXPECT_EQ("34", (BasicFormatter() << oct(034)).str()); - EXPECT_EQ("56", (BasicFormatter() << oct(056)).str()); - EXPECT_EQ("70", (BasicFormatter() << oct(070)).str()); + EXPECT_EQ("34", (BasicFormatter() << oct(034u)).str()); + EXPECT_EQ("56", (BasicFormatter() << oct(056l)).str()); + EXPECT_EQ("70", (BasicFormatter() << oct(070ul)).str()); } TEST(StrTest, hex) {