From cf1f55f7985c4dd53f32f07a34d8fad5cdb2dc61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ram=C3=ADrez?= Date: Tue, 23 Apr 2024 08:44:41 -0700 Subject: [PATCH] Specialize `formatter` for all `std::basic_string` types (#3943) * Specialize `formatter` for all `std::basic_string` types * mock-allocator: add member types to make GCC 4.8 happy --- include/fmt/format.h | 5 ++++- test/format-test.cc | 18 ++++++++++++------ test/mock-allocator.h | 10 ++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 884ba3b6..f4b2c53d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -4017,11 +4017,14 @@ FMT_FORMAT_AS(unsigned short, unsigned); FMT_FORMAT_AS(long, detail::long_type); FMT_FORMAT_AS(unsigned long, detail::ulong_type); 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::std_string_view, basic_string_view); FMT_FORMAT_AS(void*, const void*); +template +class formatter, Char> + : public formatter, Char> {}; + template struct formatter : formatter, Char> {}; diff --git a/test/format-test.cc b/test/format-test.cc index 48e2c0b2..3f5e191c 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -22,6 +22,7 @@ #include // std::back_inserter #include // std::list #include // std::mutex +#include // std::string #include // std::thread #include // std::is_default_constructible @@ -2222,16 +2223,21 @@ template void check_enabled_formatters() { } TEST(format_test, test_formatters_enabled) { + using custom_string = + std::basic_string, mock_allocator>; + using custom_wstring = std::basic_string, + mock_allocator>; + check_enabled_formatters(); - check_enabled_formatters(); + std::string, custom_string, std::nullptr_t>(); + check_enabled_formatters< + wchar_t, bool, wchar_t, signed char, unsigned char, short, unsigned short, + int, unsigned, long, unsigned long, long long, unsigned long long, float, + double, long double, void*, const void*, wchar_t*, const wchar_t*, + std::wstring, custom_wstring, std::nullptr_t>(); } TEST(format_int_test, data) { diff --git a/test/mock-allocator.h b/test/mock-allocator.h index d4dbab19..32c4caae 100644 --- a/test/mock-allocator.h +++ b/test/mock-allocator.h @@ -20,6 +20,16 @@ template class mock_allocator { using value_type = T; using size_type = size_t; + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; + using difference_type = ptrdiff_t; + + template struct rebind { + using other = mock_allocator; + }; + mock_allocator() {} mock_allocator(const mock_allocator&) {}