From fd62fba9855c9cf8fe17eb9363e03f93563d9dc3 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 9 Dec 2021 11:11:17 -0800 Subject: [PATCH] Don't convert scoped enums to integers --- include/fmt/core.h | 10 ++++++++++ include/fmt/format.h | 1 + test/core-test.cc | 3 +++ test/ranges-test.cc | 2 +- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index e87b706c4b85..179adab1247f 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -8,6 +8,7 @@ #ifndef FMT_CORE_H_ #define FMT_CORE_H_ +#include // std::byte #include // std::FILE #include #include @@ -367,6 +368,12 @@ FMT_NORETURN FMT_API void assert_fail(const char* file, int line, # endif #endif +#ifdef __cpp_lib_byte +using byte = std::byte; +#else +enum class byte : unsigned char {}; +#endif + #if defined(FMT_USE_STRING_VIEW) template using std_string_view = std::basic_string_view; #elif defined(FMT_USE_EXPERIMENTAL_STRING_VIEW) @@ -1403,6 +1410,9 @@ template struct arg_mapper { template ::value && + (std::is_convertible::value + || std::is_same::value + ) && !has_formatter::value && !has_fallback_formatter::value)> FMT_CONSTEXPR FMT_INLINE auto map(const T& val) diff --git a/include/fmt/format.h b/include/fmt/format.h index 0678f229b21b..08fba7ca13a5 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2606,6 +2606,7 @@ FMT_FORMAT_AS(unsigned long, unsigned long long); FMT_FORMAT_AS(Char*, const Char*); FMT_FORMAT_AS(std::basic_string, basic_string_view); FMT_FORMAT_AS(std::nullptr_t, const void*); +FMT_FORMAT_AS(detail::byte, unsigned char); FMT_FORMAT_AS(detail::std_string_view, basic_string_view); template diff --git a/test/core-test.cc b/test/core-test.cc index decf31a744e8..78ec3de26523 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -737,6 +737,8 @@ struct convertible_to_pointer { operator const int*() const { return nullptr; } }; +enum class test_scoped_enum {}; + TEST(core_test, is_formattable) { static_assert(fmt::is_formattable::value, ""); static_assert(fmt::is_formattable::value, ""); @@ -777,6 +779,7 @@ TEST(core_test, is_formattable) { static_assert(!fmt::is_formattable::value, ""); static_assert(!fmt::is_formattable::value, ""); + static_assert(!fmt::is_formattable::value, ""); } TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); } diff --git a/test/ranges-test.cc b/test/ranges-test.cc index f8e67390d6e2..de785c0f511f 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -190,7 +190,7 @@ TEST(ranges_test, range) { EXPECT_EQ(fmt::format("{}", z), "[0, 0, 0]"); } -enum class test_enum { foo }; +enum test_enum { foo }; TEST(ranges_test, enum_range) { auto v = std::vector{test_enum::foo};