Repository: fmtlib/fmt
File: src/os.cc (C++)

Code before:
    result_ = FormatMessageW(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
        nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        reinterpret_cast<wchar_t*>(&message_), 0, nullptr);
  }
  ~system_message() { LocalFree(message_); }
  unsigned long result() const FMT_NOEXCEPT { return result_; }
  operator wstring_view() const FMT_NOEXCEPT {
    return wstring_view(message_, result_);
  }
  wstring_view rtrim() const FMT_NOEXCEPT {
    auto len = result_;
    while (len != 0 && is_whitespace(message_[len - 1])) {
      --len;
    }
    return wstring_view(message_, len);
  }
};

class utf8_system_category final : public std::error_category {
 public:
  const char* name() const FMT_NOEXCEPT override { return "system"; }
  std::string message(int error_code) const override {
    system_message msg(error_code);
    if (msg.result() != 0) {
      utf16_to_utf8 utf8_message;
      if (utf8_message.convert(msg.rtrim()) == ERROR_SUCCESS) {
        return utf8_message.str();
      }
    }
    return "unknown error";
  }
};

}  // namespace detail

FMT_API const std::error_category& system_category() FMT_NOEXCEPT {
  static const detail::utf8_system_category category;
  return category;
}

std::system_error vwindows_error(int err_code, string_view format_str,
                                 format_args args) {
  auto ec = std::error_code(err_code, system_category());
  throw std::system_error(ec, vformat(format_str, args));
}

void detail::format_windows_error(detail::buffer<char>& out, int error_code,
                                  const char* message) FMT_NOEXCEPT {
  FMT_TRY {

Review comment (suggestion):
With explicit operator bool proposed above this can be

```c++
    if (system_message msg(error_code)) {
```

Code after:
      : result_(0), message_(nullptr) {
    result_ = FormatMessageW(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
        nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        reinterpret_cast<wchar_t*>(&message_), 0, nullptr);
    if (result_ != 0) {
      while (result_ != 0 && is_whitespace(message_[result_ - 1])) {
        --result_;
      }
    }
  }
  ~system_message() { LocalFree(message_); }
  explicit operator bool() const FMT_NOEXCEPT { return result_ != 0; }
  operator wstring_view() const FMT_NOEXCEPT {
    return wstring_view(message_, result_);
  }
};

class utf8_system_category final : public std::error_category {
 public:
  const char* name() const FMT_NOEXCEPT override { return "system"; }
  std::string message(int error_code) const override {
    system_message msg(error_code);
    if (msg) {
      utf16_to_utf8 utf8_message;
      if (utf8_message.convert(msg) == ERROR_SUCCESS) {
        return utf8_message.str();
      }
    }
    return "unknown error";
  }
};

}  // namespace detail

FMT_API const std::error_category& system_category() FMT_NOEXCEPT {
  static const detail::utf8_system_category category;
  return category;
}

std::system_error vwindows_error(int err_code, string_view format_str,
                                 format_args args) {
  auto ec = std::error_code(err_code, system_category());
  throw std::system_error(ec, vformat(format_str, args));
}

void detail::format_windows_error(detail::buffer<char>& out, int error_code,
                                  const char* message) FMT_NOEXCEPT {
  FMT_TRY {
    system_message msg(error_code);