From e690107da929117fdd0ea352c6e9d1ac388b22d7 Mon Sep 17 00:00:00 2001 From: AMAN UPADHYAY Date: Sun, 5 Jul 2026 14:51:24 +0530 Subject: [PATCH] Fix FMT_COMPILE failure with format_as mapped types (#4794) --- include/fmt/compile.h | 10 +++++++++- test/compile-test.cc | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 1a987cb0..a618e162 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -168,7 +168,13 @@ constexpr auto get_arg_checked(const Args&... args) -> const T& { template struct is_compiled_format> : std::true_type {}; -// A replacement field that refers to argument N. + +template +struct compile_has_format_as : std::false_type {}; + +template +struct compile_has_format_as()))>> : std::true_type {}; + template struct field { using char_type = Char; @@ -178,6 +184,8 @@ template struct field { if constexpr (std::is_convertible>::value) { auto s = basic_string_view(arg); return copy(s.begin(), s.end(), out); + } else if constexpr (compile_has_format_as::value) { + return write(out, format_as(arg)); } else { return write(out, arg); } diff --git a/test/compile-test.cc b/test/compile-test.cc index 5a944612..94a7da1b 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -479,3 +479,17 @@ TEST(compile_test, constexpr_string_format) { EXPECT_TRUE(big); } #endif // FMT_USE_CONSTEXPR_STRING + + + + +namespace { +struct CompileFormatAsType { + int value; +}; +int format_as(CompileFormatAsType f) { return f.value; } +} // namespace + +TEST(CompileTest, FormatAs) { + EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), CompileFormatAsType{42})); +} \ No newline at end of file