Skip to content

Commit 355be4b

Browse files
committed
Make FMT_COMPILE fallback on runtime without if constexpr (#2261)
1 parent 0cd0fb9 commit 355be4b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

include/fmt/compile.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ struct is_compiled_string : std::is_base_of<compiled_string, S> {};
114114
std::string s = fmt::format(FMT_COMPILE("{}"), 42);
115115
\endrst
116116
*/
117-
#define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string)
117+
#ifdef __cpp_if_constexpr
118+
# define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string)
119+
#else
120+
# define FMT_COMPILE(s) FMT_STRING(s)
121+
#endif
118122

119123
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
120124
template <typename Char, size_t N, fixed_string<Char, N> Str>

test/compile-test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ TEST(CompileTest, EmptyFormatString) {
155155
EXPECT_EQ(fmt::format(f), "");
156156
}
157157

158+
TEST(CompileTest, CompileFallback) {
159+
// FMT_COMPILE should fallback on runtime formatting when `if constexpr` is
160+
// not available.
161+
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42));
162+
}
163+
158164
#ifdef __cpp_if_constexpr
159165
TEST(CompileTest, FormatDefault) {
160166
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42));

0 commit comments

Comments
 (0)