Skip to content

Commit

Permalink
Make FMT_COMPILE fallback on runtime without if constexpr (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Apr 28, 2021
1 parent 0cd0fb9 commit 355be4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ struct is_compiled_string : std::is_base_of<compiled_string, S> {};
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 <typename Char, size_t N, fixed_string<Char, N> Str>
Expand Down
6 changes: 6 additions & 0 deletions test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

2 comments on commit 355be4b

@PeteTheHeat
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitaut can we get this patch in a release? Looks like 7.1.3 is from Nov 24 2020 https://github.com/fmtlib/fmt/tags

@vitaut
Copy link
Contributor Author

@vitaut vitaut commented on 355be4b Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a part of the next release.

Please sign in to comment.