Skip to content

Commit 324415c

Browse files
mwinterbvitaut
authored andcommitted
Use allocator_traits if available.
This is to avoid using functionality deprecated in C++17.
1 parent 5f39721 commit 324415c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

fmt/format.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ typedef __int64 intmax_t;
200200
# endif
201201
#endif
202202

203+
#if __cplusplus >= 201103L || FMT_MSC_VER >= 1700
204+
# define FMT_USE_ALLOCATOR_TRAITS 1
205+
#else
206+
# define FMT_USE_ALLOCATOR_TRAITS 0
207+
#endif
208+
203209
// Check if exceptions are disabled.
204210
#if defined(__GNUC__) && !defined(__EXCEPTIONS)
205211
# define FMT_EXCEPTIONS 0
@@ -869,7 +875,12 @@ void MemoryBuffer<T, SIZE, Allocator>::grow(std::size_t size) {
869875
std::size_t new_capacity = this->capacity_ + this->capacity_ / 2;
870876
if (size > new_capacity)
871877
new_capacity = size;
878+
#if FMT_USE_ALLOCATOR_TRAITS
879+
T *new_ptr =
880+
std::allocator_traits<Allocator>::allocate(*this, new_capacity, FMT_NULL);
881+
#else
872882
T *new_ptr = this->allocate(new_capacity, FMT_NULL);
883+
#endif
873884
// The following code doesn't throw, so the raw pointer above doesn't leak.
874885
std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_,
875886
make_ptr(new_ptr, new_capacity));

test/mock-allocator.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class MockAllocator {
3636
MockAllocator() {}
3737
MockAllocator(const MockAllocator &) {}
3838
typedef T value_type;
39-
MOCK_METHOD2_T(allocate, T *(std::size_t n, const T *h));
39+
MOCK_METHOD2_T(allocate, T *(std::size_t n, const void *h));
4040
MOCK_METHOD2_T(deallocate, void (T *p, std::size_t n));
4141
};
4242

@@ -78,8 +78,12 @@ class AllocatorRef {
7878

7979
Allocator *get() const { return alloc_; }
8080

81-
value_type *allocate(std::size_t n, const value_type *h) {
81+
value_type *allocate(std::size_t n, const void *h) {
82+
#if FMT_USE_ALLOCATOR_TRAITS
83+
return std::allocator_traits<Allocator>::allocate(*alloc_, n, h);
84+
#else
8285
return alloc_->allocate(n, h);
86+
#endif
8387
}
8488
void deallocate(value_type *p, std::size_t n) { alloc_->deallocate(p, n); }
8589
};

0 commit comments

Comments
 (0)