From 355be4b13fe340964a1419bd4d4c7e89db5a3174 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 28 Apr 2021 09:11:47 -0700 Subject: [PATCH] Make FMT_COMPILE fallback on runtime without if constexpr (#2261) --- include/fmt/compile.h | 6 +++++- test/compile-test.cc | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/fmt/compile.h b/include/fmt/compile.h index d231c83e..4d541c87 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -114,7 +114,11 @@ struct is_compiled_string : std::is_base_of {}; std::string s = fmt::format(FMT_COMPILE("{}"), 42); \endrst */ -#define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string) +#ifdef __cpp_if_constexpr +# define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string) +#else +# define FMT_COMPILE(s) FMT_STRING(s) +#endif #if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS template Str> diff --git a/test/compile-test.cc b/test/compile-test.cc index 56056ec4..31db499a 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -155,6 +155,12 @@ TEST(CompileTest, EmptyFormatString) { EXPECT_EQ(fmt::format(f), ""); } +TEST(CompileTest, CompileFallback) { + // FMT_COMPILE should fallback on runtime formatting when `if constexpr` is + // not available. + EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42)); +} + #ifdef __cpp_if_constexpr TEST(CompileTest, FormatDefault) { EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42));