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

Supports formatting std::expected<void, E> #4145

Closed
Hackerl opened this issue Sep 3, 2024 · 0 comments
Closed

Supports formatting std::expected<void, E> #4145

Hackerl opened this issue Sep 3, 2024 · 0 comments

Comments

@Hackerl
Copy link

Hackerl commented Sep 3, 2024

Consider adding a formatter for std::expected<void, E>

template <typename E, typename Char>
struct formatter<std::expected<void, E>, Char,
                 std::enable_if_t<is_formattable<E, Char>::value>> {
  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
    return ctx.begin();
  }

  template <typename FormatContext>
  auto format(const std::expected<void, E>& value, FormatContext& ctx) const
      -> decltype(ctx.out()) {
    auto out = ctx.out();

    if (value.has_value()) {
      out = detail::write<Char>(out, "expected(");
    } else {
      out = detail::write<Char>(out, "unexpected(");
      out = detail::write_escaped_alternative<Char>(out, value.error());
    }
    *out++ = ')';
    return out;
  }
};

Output expected() when there is no error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants