From 21a3a3699922eee7f78c4a086f4a6b6af806c5b5 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Wed, 4 Sep 2024 20:05:23 +0500 Subject: [PATCH 1/2] Make is_formattable work with const/volatile void Signed-off-by: Vladislav Shchapov --- include/fmt/base.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 64c6a07bb713..707b0ad196b3 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -2819,11 +2819,11 @@ template class fstring { template using format_string = typename fstring::t; template -using is_formattable = bool_constant::value, detail::unformattable, T>, - Char>>::value>; +using is_formattable = bool_constant< + !std::is_base_of::value, + detail::unformattable, T>, + Char>>::value>; #ifdef __cpp_concepts template From 4374e1dc03aa387f2da28468975aa955363ceb83 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Tue, 3 Sep 2024 21:44:10 +0500 Subject: [PATCH 2/2] Add support formatting std::expected Signed-off-by: Vladislav Shchapov --- include/fmt/std.h | 6 ++++-- test/std-test.cc | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index 28460846db12..00eee38f9644 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -279,7 +279,8 @@ FMT_BEGIN_NAMESPACE FMT_EXPORT template struct formatter, Char, - std::enable_if_t::value && + std::enable_if_t<(std::is_void::value || + is_formattable::value) && is_formattable::value>> { FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { return ctx.begin(); @@ -292,7 +293,8 @@ struct formatter, Char, if (value.has_value()) { out = detail::write(out, "expected("); - out = detail::write_escaped_alternative(out, *value); + if constexpr (!std::is_void::value) + out = detail::write_escaped_alternative(out, *value); } else { out = detail::write(out, "unexpected("); out = detail::write_escaped_alternative(out, value.error()); diff --git a/test/std-test.cc b/test/std-test.cc index 424441816501..5dad5fb2057b 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -140,6 +140,7 @@ TEST(std_test, optional) { TEST(std_test, expected) { #ifdef __cpp_lib_expected + EXPECT_EQ(fmt::format("{}", std::expected{}), "expected()"); EXPECT_EQ(fmt::format("{}", std::expected{1}), "expected(1)"); EXPECT_EQ(fmt::format("{}", std::expected{std::unexpected(1)}), "unexpected(1)"); @@ -163,6 +164,7 @@ TEST(std_test, expected) { EXPECT_FALSE( (fmt::is_formattable>::value)); EXPECT_TRUE((fmt::is_formattable>::value)); + EXPECT_TRUE((fmt::is_formattable>::value)); #endif }