From cdfcee27fbb22b22ef616cddf30352191da9c14f Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 10 Feb 2018 06:51:13 -0800 Subject: [PATCH] Use allocator_traits if available --- include/fmt/format.h | 11 ++++++++++- test/mock-allocator.h | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index fde7ab4f..2c3266ce 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -222,7 +222,16 @@ inline dummy_int isinf(...) { return dummy_int(); } inline dummy_int _finite(...) { return dummy_int(); } inline dummy_int isnan(...) { return dummy_int(); } inline dummy_int _isnan(...) { return dummy_int(); } + +template +typename Allocator::value_type *allocate(Allocator& alloc, std::size_t n) { +#if __cplusplus >= 201103L || FMT_MSC_VER >= 1700 + return std::allocator_traits::allocate(alloc, n); +#else + return this->allocate(n); +#endif } +} // namespace internal } // namespace fmt namespace std { @@ -442,7 +451,7 @@ void basic_memory_buffer::grow(std::size_t size) { if (size > new_capacity) new_capacity = size; T *old_data = this->data(); - T *new_data = this->allocate(new_capacity); + T *new_data = internal::allocate(*this, new_capacity); // The following code doesn't throw, so the raw pointer above doesn't leak. std::uninitialized_copy(old_data, old_data + this->size(), internal::make_ptr(new_data, new_capacity)); diff --git a/test/mock-allocator.h b/test/mock-allocator.h index 7de1e1a0..96a41352 100644 --- a/test/mock-allocator.h +++ b/test/mock-allocator.h @@ -78,7 +78,9 @@ class AllocatorRef { Allocator *get() const { return alloc_; } - value_type* allocate(std::size_t n) { return alloc_->allocate(n); } + value_type* allocate(std::size_t n) { + return fmt::internal::allocate(*alloc_, n); + } void deallocate(value_type* p, std::size_t n) { alloc_->deallocate(p, n); } };