Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support formatting std::expected<void, E> #4148

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2819,11 +2819,11 @@ template <typename Char, typename... T> class fstring {
template <typename... T> using format_string = typename fstring<char, T...>::t;

template <typename T, typename Char = char>
using is_formattable = bool_constant<!std::is_base_of<
detail::unformattable,
detail::mapped_t<
conditional_t<std::is_same<T, void>::value, detail::unformattable, T>,
Char>>::value>;
using is_formattable = bool_constant<
!std::is_base_of<detail::unformattable,
detail::mapped_t<conditional_t<std::is_void<T>::value,
detail::unformattable, T>,
Char>>::value>;

#ifdef __cpp_concepts
template <typename T, typename Char = char>
Expand Down
6 changes: 4 additions & 2 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ FMT_BEGIN_NAMESPACE
FMT_EXPORT
template <typename T, typename E, typename Char>
struct formatter<std::expected<T, E>, Char,
std::enable_if_t<is_formattable<T, Char>::value &&
std::enable_if_t<(std::is_void<T>::value ||
is_formattable<T, Char>::value) &&
is_formattable<E, Char>::value>> {
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
return ctx.begin();
Expand All @@ -292,7 +293,8 @@ struct formatter<std::expected<T, E>, Char,

if (value.has_value()) {
out = detail::write<Char>(out, "expected(");
out = detail::write_escaped_alternative<Char>(out, *value);
if constexpr (!std::is_void<T>::value)
out = detail::write_escaped_alternative<Char>(out, *value);
} else {
out = detail::write<Char>(out, "unexpected(");
out = detail::write_escaped_alternative<Char>(out, value.error());
Expand Down
2 changes: 2 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ TEST(std_test, optional) {

TEST(std_test, expected) {
#ifdef __cpp_lib_expected
EXPECT_EQ(fmt::format("{}", std::expected<void, int>{}), "expected()");
EXPECT_EQ(fmt::format("{}", std::expected<int, int>{1}), "expected(1)");
EXPECT_EQ(fmt::format("{}", std::expected<int, int>{std::unexpected(1)}),
"unexpected(1)");
Expand All @@ -163,6 +164,7 @@ TEST(std_test, expected) {
EXPECT_FALSE(
(fmt::is_formattable<std::expected<int, unformattable2>>::value));
EXPECT_TRUE((fmt::is_formattable<std::expected<int, int>>::value));
EXPECT_TRUE((fmt::is_formattable<std::expected<void, int>>::value));
#endif
}

Expand Down