Don't convert scoped enums to integers

This commit is contained in:
Victor Zverovich 2021-12-09 11:11:17 -08:00
parent c652f8243a
commit fd62fba985
4 changed files with 15 additions and 1 deletions

View File

@ -8,6 +8,7 @@
#ifndef FMT_CORE_H_
#define FMT_CORE_H_
#include <cstddef> // std::byte
#include <cstdio> // std::FILE
#include <cstring>
#include <iterator>
@ -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 <typename Char> using std_string_view = std::basic_string_view<Char>;
#elif defined(FMT_USE_EXPERIMENTAL_STRING_VIEW)
@ -1403,6 +1410,9 @@ template <typename Context> struct arg_mapper {
template <typename T,
FMT_ENABLE_IF(std::is_enum<T>::value &&
(std::is_convertible<T, int>::value
|| std::is_same<T, detail::byte>::value
) &&
!has_formatter<T, Context>::value &&
!has_fallback_formatter<T, char_type>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val)

View File

@ -2606,6 +2606,7 @@ FMT_FORMAT_AS(unsigned long, unsigned long long);
FMT_FORMAT_AS(Char*, const Char*);
FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);
FMT_FORMAT_AS(std::nullptr_t, const void*);
FMT_FORMAT_AS(detail::byte, unsigned char);
FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
template <typename Char>

View File

@ -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<signed char*>::value, "");
static_assert(fmt::is_formattable<unsigned char*>::value, "");
@ -777,6 +779,7 @@ TEST(core_test, is_formattable) {
static_assert(!fmt::is_formattable<int(s::*)>::value, "");
static_assert(!fmt::is_formattable<int (s::*)()>::value, "");
static_assert(!fmt::is_formattable<test_scoped_enum>::value, "");
}
TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); }

View File

@ -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>{test_enum::foo};