Enable format_as for classes

This commit is contained in:
Victor Zverovich 2023-01-02 10:53:41 -08:00
parent b7535365b2
commit 9121f9b1d3
2 changed files with 11 additions and 1 deletions

View File

@ -1348,10 +1348,16 @@ inline auto format_as(std::byte b) -> unsigned char {
} }
#endif #endif
template <typename T> struct convertible_to { operator const T&() const; };
template <typename T> struct has_format_as { template <typename T> struct has_format_as {
template <typename U, typename V = decltype(format_as(U())), template <typename U, typename V = decltype(format_as(U())),
FMT_ENABLE_IF(std::is_enum<U>::value)> FMT_ENABLE_IF(std::is_enum<U>::value)>
static auto check(U*) -> std::true_type; static auto check(U*) -> std::true_type;
// Use convertible_to to avoid implicit conversions.
template <typename U, typename V = decltype(format_as(convertible_to<U>())),
FMT_ENABLE_IF(std::is_class<U>::value)>
static auto check(U*) -> std::true_type;
static auto check(...) -> std::false_type; static auto check(...) -> std::false_type;
enum { value = decltype(check(static_cast<T*>(nullptr)))::value }; enum { value = decltype(check(static_cast<T*>(nullptr)))::value };

View File

@ -680,6 +680,9 @@ auto format_as(scoped_enum_as_int) -> int { return 42; }
enum class scoped_enum_as_string {}; enum class scoped_enum_as_string {};
auto format_as(scoped_enum_as_string) -> fmt::string_view { return "foo"; } auto format_as(scoped_enum_as_string) -> fmt::string_view { return "foo"; }
struct struct_as_int {};
auto format_as(struct_as_int) -> int { return 42; }
struct convertible_to_enum { struct convertible_to_enum {
operator scoped_enum_as_int() const { return {}; } operator scoped_enum_as_int() const { return {}; }
}; };
@ -724,7 +727,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<unformattable_scoped_enum>::value, ""); static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, "");
static_assert(fmt::is_formattable<test::scoped_enum_as_int>::value, ""); static_assert(fmt::is_formattable<test::scoped_enum_as_int>::value, "");
static_assert(!fmt::is_formattable<test::convertible_to_enum>::value, ""); static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, "");
} }
TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); } TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); }
@ -738,6 +741,7 @@ TEST(core_test, format_to) {
TEST(core_test, format_as) { TEST(core_test, format_as) {
EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_int()), "42"); EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_int()), "42");
EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string()), "foo"); EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string()), "foo");
EXPECT_EQ(fmt::format("{}", test::struct_as_int()), "42");
} }
#ifdef __cpp_lib_byte #ifdef __cpp_lib_byte