Skip to content

Commit a97a560

Browse files
dieram3Arghnews
authored andcommitted
Specialize formatter for all std::basic_string types (fmtlib#3943)
* Specialize `formatter` for all `std::basic_string` types * mock-allocator: add member types to make GCC 4.8 happy
1 parent b1df20a commit a97a560

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

include/fmt/format.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -4017,11 +4017,14 @@ FMT_FORMAT_AS(unsigned short, unsigned);
40174017
FMT_FORMAT_AS(long, detail::long_type);
40184018
FMT_FORMAT_AS(unsigned long, detail::ulong_type);
40194019
FMT_FORMAT_AS(Char*, const Char*);
4020-
FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);
40214020
FMT_FORMAT_AS(std::nullptr_t, const void*);
40224021
FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
40234022
FMT_FORMAT_AS(void*, const void*);
40244023

4024+
template <typename Char, typename Traits, typename Allocator>
4025+
class formatter<std::basic_string<Char, Traits, Allocator>, Char>
4026+
: public formatter<basic_string_view<Char>, Char> {};
4027+
40254028
template <typename Char, size_t N>
40264029
struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {};
40274030

test/format-test.cc

+12-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <iterator> // std::back_inserter
2323
#include <list> // std::list
2424
#include <mutex> // std::mutex
25+
#include <string> // std::string
2526
#include <thread> // std::thread
2627
#include <type_traits> // std::is_default_constructible
2728

@@ -2222,16 +2223,21 @@ template <typename Char, typename... T> void check_enabled_formatters() {
22222223
}
22232224

22242225
TEST(format_test, test_formatters_enabled) {
2226+
using custom_string =
2227+
std::basic_string<char, std::char_traits<char>, mock_allocator<char>>;
2228+
using custom_wstring = std::basic_string<wchar_t, std::char_traits<wchar_t>,
2229+
mock_allocator<wchar_t>>;
2230+
22252231
check_enabled_formatters<char, bool, char, signed char, unsigned char, short,
22262232
unsigned short, int, unsigned, long, unsigned long,
22272233
long long, unsigned long long, float, double,
22282234
long double, void*, const void*, char*, const char*,
2229-
std::string, std::nullptr_t>();
2230-
check_enabled_formatters<wchar_t, bool, wchar_t, signed char, unsigned char,
2231-
short, unsigned short, int, unsigned, long,
2232-
unsigned long, long long, unsigned long long, float,
2233-
double, long double, void*, const void*, wchar_t*,
2234-
const wchar_t*, std::wstring, std::nullptr_t>();
2235+
std::string, custom_string, std::nullptr_t>();
2236+
check_enabled_formatters<
2237+
wchar_t, bool, wchar_t, signed char, unsigned char, short, unsigned short,
2238+
int, unsigned, long, unsigned long, long long, unsigned long long, float,
2239+
double, long double, void*, const void*, wchar_t*, const wchar_t*,
2240+
std::wstring, custom_wstring, std::nullptr_t>();
22352241
}
22362242

22372243
TEST(format_int_test, data) {

test/mock-allocator.h

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ template <typename T> class mock_allocator {
2020
using value_type = T;
2121
using size_type = size_t;
2222

23+
using pointer = T*;
24+
using const_pointer = const T*;
25+
using reference = T&;
26+
using const_reference = const T&;
27+
using difference_type = ptrdiff_t;
28+
29+
template <typename U> struct rebind {
30+
using other = mock_allocator<U>;
31+
};
32+
2333
mock_allocator() {}
2434
mock_allocator(const mock_allocator&) {}
2535

0 commit comments

Comments
 (0)