Fix FMT_COMPILE failure with format_as mapped types (Issue #4794) (#4836)

This commit is contained in:
AMAN UPADHYAY 2026-07-12 21:51:55 +05:30 committed by GitHub
parent 1a99c2630c
commit af2d9f2b78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 0 deletions

View File

@ -178,6 +178,10 @@ template <typename Char, typename V, int N> struct field {
if constexpr (std::is_convertible<V, basic_string_view<Char>>::value) { if constexpr (std::is_convertible<V, basic_string_view<Char>>::value) {
auto s = basic_string_view<Char>(arg); auto s = basic_string_view<Char>(arg);
return copy<Char>(s.begin(), s.end(), out); return copy<Char>(s.begin(), s.end(), out);
} else if constexpr (use_format_as<V>::value) {
return write<Char>(out, format_as(arg));
} else if constexpr (use_format_as_member<V>::value) {
return write<Char>(out, formatter<V>::format_as(arg));
} else { } else {
return write<Char>(out, arg); return write<Char>(out, arg);
} }

View File

@ -479,3 +479,13 @@ TEST(compile_test, constexpr_string_format) {
EXPECT_TRUE(big); EXPECT_TRUE(big);
} }
#endif // FMT_USE_CONSTEXPR_STRING #endif // FMT_USE_CONSTEXPR_STRING
namespace {
struct compile_format_as_type {
int value;
};
int format_as(compile_format_as_type f) { return f.value; }
}
TEST(compile_test, format_as) {
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), compile_format_as_type{42}));
}