From 22d50c1a9c23a65ce56086a3eb74dbe29aede957 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Tue, 3 Sep 2024 21:44:10 +0500 Subject: [PATCH] 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 28460846..00eee38f 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 42444181..5dad5fb2 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 }