Neargye / magic_enum

Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code
MIT License
4.99k stars 445 forks source link

libfmt support #259

Closed Timple closed 1 year ago

Timple commented 1 year ago

Good day

Unfortunately we cannot leverage the power of std::format just yet as we're stuck on ubuntu 22.04 for now.

However, libfmt is the predecesor of this format. Supporting this can be done easily with this snippet:

#include <fmt/format.h>
#include <magic_enum.hpp>

template <typename E>
struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<E>, char>> : fmt::formatter<std::string_view, char> {
  auto format(E e, format_context& ctx)
  {
    return formatter<string_view>::format(magic_enum::enum_name(e), ctx);
  }
};

Could this perhaps be included in this wonderful library? Similar to the std::format perhaps?

Neargye commented 1 year ago

added in master

Timple commented 1 year ago

Awesome! Thanks for the quick update and for the continued maintenance of this project!