mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-29 00:35:03 +00:00
Add fmt::enums::format_as
This commit is contained in:
parent
1319719a5e
commit
aad44f2839
@ -2891,6 +2891,13 @@ constexpr auto underlying(Enum e) noexcept -> underlying_t<Enum> {
|
||||
return static_cast<underlying_t<Enum>>(e);
|
||||
}
|
||||
|
||||
namespace enums {
|
||||
template <typename Enum, FMT_ENABLE_IF(std::is_enum<Enum>::value)>
|
||||
constexpr auto format_as(Enum e) noexcept -> underlying_t<Enum> {
|
||||
return static_cast<underlying_t<Enum>>(e);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cpp_lib_byte
|
||||
inline auto format_as(std::byte b) -> unsigned char { return underlying(b); }
|
||||
FMT_FORMAT_AS(std::byte, unsigned char);
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user