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));