diff --git a/fmt/format.h b/fmt/format.h index a284468b..dc8ce0c9 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -371,6 +371,9 @@ class BasicWriter; typedef BasicWriter Writer; typedef BasicWriter WWriter; +template +class basic_format_arg; + template class ArgFormatter; @@ -1329,6 +1332,9 @@ class MakeValue : public Value { template class ArgMap; + +template +basic_format_arg make_arg(const T &value); } // namespace internal struct monostate {}; @@ -1340,10 +1346,14 @@ class basic_format_args; // allow storage in internal::MemoryBuffer. template class basic_format_arg { - protected: + private: internal::Value value_; internal::Type type_; + template + friend basic_format_arg + internal::make_arg(const T &value); + template friend typename std::result_of::type visit(Visitor &&vis, basic_format_arg arg); @@ -1426,19 +1436,13 @@ typename std::result_of::type namespace internal { -template - class MakeArg : public basic_format_arg { - public: - MakeArg() { - this->type_ = internal::NONE; - } - - template - MakeArg(const T &value) { - this->value_ = internal::MakeValue(value); - this->type_ = internal::type(); - } -}; +template +basic_format_arg make_arg(const T &value) { + basic_format_arg arg; + arg.type_ = internal::type(); + arg.value_ = internal::MakeValue(value); + return arg; +} template struct LConvCheck { LConvCheck(int) {} @@ -1490,7 +1494,7 @@ struct NamedArg : basic_format_arg { template NamedArg(BasicStringRef argname, const T &value) - : basic_format_arg(MakeArg< basic_format_context >(value)), + : basic_format_arg(make_arg< basic_format_context >(value)), name(argname) {} }; @@ -1522,7 +1526,7 @@ template inline typename std::enable_if< !IS_PACKED, basic_format_arg>::type make_arg(const T& value) { - return MakeArg(value); + return make_arg(value); } } // namespace internal diff --git a/fmt/ostream.h b/fmt/ostream.h index c623b542..74ccadf4 100644 --- a/fmt/ostream.h +++ b/fmt/ostream.h @@ -87,8 +87,8 @@ void format_value(BasicWriter &w, const T &value, basic_format_context &ctx) { internal::MemoryBuffer buffer; auto str = internal::format_value(buffer, value); - typedef internal::MakeArg< basic_format_context > MakeArg; - do_format_arg< ArgFormatter >(w, MakeArg(str), ctx); + do_format_arg< ArgFormatter >( + w, internal::make_arg< basic_format_context >(str), ctx); } FMT_API void vprint(std::ostream &os, CStringRef format_str, format_args args); diff --git a/fmt/printf.h b/fmt/printf.h index 47d868e6..c0064ae7 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -105,11 +105,11 @@ class ArgConverter { if (sizeof(TargetType) <= sizeof(int)) { // Extra casts are used to silence warnings. if (is_signed) { - arg_ = internal::MakeArg( + arg_ = internal::make_arg( static_cast(static_cast(value))); } else { typedef typename internal::MakeUnsigned::Type Unsigned; - arg_ = internal::MakeArg( + arg_ = internal::make_arg( static_cast(static_cast(value))); } } else { @@ -117,10 +117,10 @@ class ArgConverter { // glibc's printf doesn't sign extend arguments of smaller types: // std::printf("%lld", -42); // prints "4294967254" // but we don't have to do the same because it's a UB. - arg_ = internal::MakeArg( + arg_ = internal::make_arg( static_cast(value)); } else { - arg_ = internal::MakeArg( + arg_ = internal::make_arg( static_cast::Type>(value)); } } @@ -157,7 +157,7 @@ class CharConverter { typename std::enable_if::value>::type operator()(T value) { arg_ = - internal::MakeArg>(static_cast(value)); + internal::make_arg>(static_cast(value)); } template diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index ff702a53..8a0afb02 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -57,7 +57,7 @@ struct ValueExtractor { TEST(FormatTest, ArgConverter) { using fmt::format_arg; fmt::LongLong value = std::numeric_limits::max(); - format_arg arg = fmt::internal::MakeArg(value); + format_arg arg = fmt::internal::make_arg(value); visit(fmt::internal::ArgConverter(arg, 'd'), arg); EXPECT_EQ(value, visit(ValueExtractor(), arg)); } diff --git a/test/ostream-test.cc b/test/ostream-test.cc index b46418b4..069c8092 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -69,7 +69,7 @@ TEST(OStreamTest, CustomArg) { fmt::format_context ctx("}", fmt::format_args()); fmt::FormatSpec spec; TestArgFormatter af(writer, ctx, spec); - visit(af, fmt::internal::MakeArg(TestEnum())); + visit(af, fmt::internal::make_arg(TestEnum())); EXPECT_EQ("TestEnum", writer.str()); } diff --git a/test/util-test.cc b/test/util-test.cc index 69569e43..e02b68d8 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -75,8 +75,7 @@ void format_value(fmt::BasicWriter &w, Test, template basic_format_arg make_arg(const T &value) { - typedef fmt::internal::MakeArg< fmt::basic_format_context > MakeArg; - return MakeArg(value); + return fmt::internal::make_arg< fmt::basic_format_context >(value); } } // namespace