From c460e73c366fa9282fcbae581b2af42f103bb7c9 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 20 Sep 2024 07:08:47 -0700 Subject: [PATCH] Add member format_as for std --- include/fmt/base.h | 31 +++++++++++++++++-------------- include/fmt/format.h | 12 ++++++++++++ test/format-test.cc | 2 ++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index a4af4f4c9e17d..734de0daa30c9 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -17,8 +17,6 @@ # include // FILE # include // memcmp -// is also included transitively from . -# include // std::byte # include // std::enable_if #endif @@ -643,15 +641,6 @@ struct formatter { formatter() = delete; }; -// This is defined in base.h instead of format.h to avoid injecting in std. -// It is a template to avoid undesirable implicit conversions to std::byte. -#ifdef __cpp_lib_byte -template ::value)> -inline auto format_as(T b) -> unsigned char { - return static_cast(b); -} -#endif - /// Reports a format error at compile time or, via a `format_error` exception, /// at runtime. // This function is intentionally not constexpr to give a compile-time error. @@ -1071,14 +1060,24 @@ using ulong_type = conditional_t; template using format_as_result = remove_cvref_t()))>; +template +using format_as_member_result = + remove_cvref_t::format_as(std::declval()))>; template struct use_format_as : std::false_type {}; +// format_as member is only used to avoid injection into the std namespace. +template +struct use_format_as_member : std::false_type {}; // Only map owning types because mapping views can be unsafe. template struct use_format_as< - T, bool_constant>::value>> + T, bool_constant>::value>> + : std::true_type {}; +template +struct use_format_as_member< + T, bool_constant>::value>> : std::true_type {}; template > @@ -1086,7 +1085,7 @@ using use_formatter = bool_constant<(std::is_class::value || std::is_enum::value || std::is_union::value || std::is_array::value) && !has_to_string_view::value && !is_named_arg::value && - !use_format_as::value>; + !use_format_as::value && !use_format_as_member::value>; template > auto has_formatter_impl(T* p, buffered_context* ctx = nullptr) @@ -1147,6 +1146,8 @@ template struct type_mapper { template ::value)> static auto map(const T& x) -> decltype(map(format_as(x))); + template ::value)> + static auto map(const T& x) -> decltype(map(formatter::format_as(x))); template ::value)> static auto map(T&) -> conditional_t(), T&, void>; @@ -2144,6 +2145,8 @@ template class value { template ::value)> value(const T& x) : value(format_as(x)) {} + template ::value)> + value(const T& x) : value(formatter::format_as(x)) {} template ::value)> value(const T& named_arg) : value(named_arg.value) {} @@ -2376,7 +2379,7 @@ template class basic_appender { public: using iterator_category = int; using value_type = T; - using difference_type = ptrdiff_t; + using difference_type = decltype(std::declval() - std::declval()); using pointer = T*; using reference = T&; using container_type = detail::buffer; diff --git a/include/fmt/format.h b/include/fmt/format.h index 58f52990a91b9..398829f4d795d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3919,6 +3919,18 @@ constexpr auto format_as(Enum e) noexcept -> underlying_t { } } // namespace enums +#ifdef __cpp_lib_byte +template <> struct formatter : formatter { + static auto format_as(std::byte b) -> unsigned char { + return static_cast(b); + } + template + auto format(std::byte b, Context& ctx) const -> decltype(ctx.out()) { + return formatter::format(format_as(b), ctx); + } +}; +#endif + class bytes { private: string_view data_; diff --git a/test/format-test.cc b/test/format-test.cc index 73186d0243c14..1c907c87183e6 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2030,6 +2030,8 @@ enum big_enum : unsigned long long { big_enum_value = 5000000000ULL }; auto format_as(big_enum e) -> unsigned long long { return e; } TEST(format_test, strong_enum) { + auto arg = fmt::basic_format_arg(big_enum_value); + EXPECT_EQ(arg.type(), fmt::detail::type::ulong_long_type); EXPECT_EQ(fmt::format("{}", big_enum_value), "5000000000"); } #endif