Skip to content

Commit 47c2e35

Browse files
committed
[libc++] Specialize allocator_traits for std::allocator
1 parent 3a95bf4 commit 47c2e35

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

libcxx/include/__memory/allocator_traits.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,80 @@ struct allocator_traits {
336336
}
337337
};
338338

339+
template <class _Tp>
340+
struct allocator_traits<allocator<_Tp>> {
341+
using allocator_type = allocator<_Tp>;
342+
using value_type = _Tp;
343+
using pointer = _Tp*;
344+
using const_pointer = const _Tp*;
345+
using void_pointer = void*;
346+
using const_void_pointer = const void*;
347+
using difference_type = ptrdiff_t;
348+
using size_type = size_t;
349+
using propagate_on_container_copy_assignment = false_type;
350+
using propagate_on_container_move_assignment = true_type;
351+
using propagate_on_container_swap = false_type;
352+
using is_always_equal = true_type;
353+
354+
#ifndef _LIBCPP_CXX03_LANG
355+
template <class _Up>
356+
using rebind_alloc = allocator<_Up>;
357+
template <class _Up>
358+
using rebind_traits = allocator_traits<allocator<_Up>>;
359+
#else
360+
template <class _Up>
361+
struct rebind_alloc {
362+
using other = allocator<_Up>;
363+
};
364+
template <class _Up>
365+
struct rebind_traits {
366+
using other = allocator_traits<allocator<_Up> >;
367+
};
368+
#endif
369+
370+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
371+
_LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer allocate(allocator_type& __alloc, size_type __n) {
372+
return __alloc.allocate(__n);
373+
}
374+
375+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
376+
allocate(allocator_type& __alloc, size_type __n, const_void_pointer) {
377+
return __alloc.allocate(__n);
378+
}
379+
380+
#if _LIBCPP_STD_VER >= 23
381+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr allocation_result<pointer, size_type>
382+
allocate_at_least(allocator_type& __alloc, size_type __n) {
383+
return {__alloc.allocate(__n), __n};
384+
}
385+
#endif
386+
387+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
388+
deallocate(allocator_type& __alloc, pointer __ptr, size_type __n) _NOEXCEPT {
389+
__alloc.deallocate(__ptr, __n);
390+
}
391+
392+
template <class _Up, class... _Args>
393+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
394+
construct(allocator_type&, _Up* __ptr, _Args&&... __args) {
395+
std::__construct_at(__ptr, std::forward<_Args>(__args)...);
396+
}
397+
398+
template <class _Up>
399+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type&, _Up* __ptr) {
400+
std::__destroy_at(__ptr);
401+
}
402+
403+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type max_size(const allocator_type&) _NOEXCEPT {
404+
return numeric_limits<size_type>::max() / sizeof(value_type);
405+
}
406+
407+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
408+
select_on_container_copy_construction(const allocator_type&) _NOEXCEPT {
409+
return allocator_type();
410+
}
411+
};
412+
339413
#ifndef _LIBCPP_CXX03_LANG
340414
template <class _Traits, class _Tp>
341415
using __rebind_alloc _LIBCPP_NODEBUG = typename _Traits::template rebind_alloc<_Tp>;

0 commit comments

Comments
 (0)