From da0ea4161a3be6c0a2e4e997f13bb88dec9c09be Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 21 Mar 2019 19:02:19 -0700 Subject: [PATCH] Make compile-time checks work with fallback formatter (#1088) --- include/fmt/core.h | 17 +++++++++++------ include/fmt/format.h | 5 ++++- test/ostream-test.cc | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 9c63517c..f71fdb01 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -674,6 +674,15 @@ template struct custom_value { Context& ctx); }; +template +struct is_formattable { + enum { + value = !is_constructible< + typename Context::template formatter_type::type, + internal::dummy_formatter_arg>::value + }; +}; + // A formatting argument value. template class value { public: @@ -721,14 +730,10 @@ template class value { // Get the formatter type through the context to allow different contexts // have different extension points, e.g. `formatter` for `format` and // `printf_formatter` for `printf`. - typedef typename Context::template formatter_type::type formatter; - enum { - formattable = - !is_constructible::value - }; custom.format = &format_custom_arg< T, typename std::conditional< - formattable, formatter, + is_formattable::value, + typename Context::template formatter_type::type, internal::fallback_formatter>::type>; } diff --git a/include/fmt/format.h b/include/fmt/format.h index 0a251ce0..669f4bfe 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2136,7 +2136,10 @@ template FMT_CONSTEXPR const typename ParseContext::char_type* parse_format_specs( ParseContext& ctx) { // GCC 7.2 requires initializer. - formatter f{}; + typedef typename ParseContext::char_type char_type; + typename std::conditional< + is_formattable::value, + formatter, internal::fallback_formatter>::type f; return f.parse(ctx); } diff --git a/test/ostream-test.cc b/test/ostream-test.cc index 6abfcf30..a682e537 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -173,6 +173,7 @@ TEST(OStreamTest, Join) { #if FMT_USE_CONSTEXPR TEST(OStreamTest, ConstexprString) { EXPECT_EQ("42", format(fmt("{}"), std::string("42"))); + EXPECT_EQ("a string", format(fmt("{0}"), TestString("a string"))); } #endif