From aad44f28396b8cf9722d74027b94840820f7c3dc Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 19 Feb 2022 08:03:42 -0800 Subject: [PATCH] Add fmt::enums::format_as --- include/fmt/format.h | 7 +++++++ test/format-test.cc | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/include/fmt/format.h b/include/fmt/format.h index dcd1d58a..1db4528d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2891,6 +2891,13 @@ constexpr auto underlying(Enum e) noexcept -> underlying_t { return static_cast>(e); } +namespace enums { +template ::value)> +constexpr auto format_as(Enum e) noexcept -> underlying_t { + return static_cast>(e); +} +} + #ifdef __cpp_lib_byte inline auto format_as(std::byte b) -> unsigned char { return underlying(b); } FMT_FORMAT_AS(std::byte, unsigned char); diff --git a/test/format-test.cc b/test/format-test.cc index c7a468ad..3b540ab8 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1459,8 +1459,14 @@ TEST(format_test, write_uintptr_fallback) { enum class color { red, green, blue }; +namespace test_ns { +enum class color { red, green, blue }; +using fmt::enums::format_as; +} // namespace test_ns + TEST(format_test, format_enum_class) { EXPECT_EQ(fmt::format("{}", fmt::underlying(color::red)), "0"); + EXPECT_EQ(fmt::format("{}", test_ns::color::red), "0"); } TEST(format_test, format_string) {