libstdc++
bits/alloc_traits.h
Go to the documentation of this file.
1// Allocator traits -*- C++ -*-
2
3// Copyright (C) 2011-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/alloc_traits.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
28 */
29
30#ifndef _ALLOC_TRAITS_H
31#define _ALLOC_TRAITS_H 1
32
33#include <bits/stl_construct.h>
34#include <bits/memoryfwd.h>
35#if __cplusplus >= 201103L
36# include <bits/ptr_traits.h>
37# include <ext/numeric_traits.h>
38# if _GLIBCXX_HOSTED
39# include <bits/allocator.h>
40# endif
41# if __cpp_exceptions
42# include <bits/stl_iterator.h> // __make_move_if_noexcept_iterator
43# endif
44#endif
45
46namespace std _GLIBCXX_VISIBILITY(default)
47{
48_GLIBCXX_BEGIN_NAMESPACE_VERSION
49
50#if __cplusplus >= 201103L
51
52#pragma GCC diagnostic push
53#pragma GCC diagnostic ignored "-Wc++14-extensions" // for variable templates
54#pragma GCC diagnostic ignored "-Wc++17-extensions" // for if-constexpr
55
56 /// @cond undocumented
57 struct __allocator_traits_base
58 {
59#if __cpp_concepts
60 template<typename _Tp, typename _Up>
61#else
62 template<typename _Tp, typename _Up, typename = void>
63#endif
64 struct __rebind : __replace_first_arg<_Tp, _Up>
65 {
66 static_assert(is_same<
67 typename __replace_first_arg<_Tp, typename _Tp::value_type>::type,
68 _Tp>::value,
69 "allocator_traits<A>::rebind_alloc<A::value_type> must be A");
70 };
71
72 template<typename _Tp, typename _Up>
73#if __cpp_concepts
74 requires requires { typename _Tp::template rebind<_Up>::other; }
75 struct __rebind<_Tp, _Up>
76#else
77 struct __rebind<_Tp, _Up,
78 __void_t<typename _Tp::template rebind<_Up>::other>>
79#endif
80 {
81 using type = typename _Tp::template rebind<_Up>::other;
82
83 static_assert(is_same<
84 typename _Tp::template rebind<typename _Tp::value_type>::other,
85 _Tp>::value,
86 "allocator_traits<A>::rebind_alloc<A::value_type> must be A");
87 };
88
89 protected:
90 template<typename _Tp>
91 using __pointer = typename _Tp::pointer;
92 template<typename _Tp>
93 using __c_pointer = typename _Tp::const_pointer;
94 template<typename _Tp>
95 using __v_pointer = typename _Tp::void_pointer;
96 template<typename _Tp>
97 using __cv_pointer = typename _Tp::const_void_pointer;
98 template<typename _Tp>
99 using __pocca = typename _Tp::propagate_on_container_copy_assignment;
100 template<typename _Tp>
101 using __pocma = typename _Tp::propagate_on_container_move_assignment;
102 template<typename _Tp>
103 using __pocs = typename _Tp::propagate_on_container_swap;
104 template<typename _Tp>
105 using __equal = __type_identity<typename _Tp::is_always_equal>;
106
107 // __has_allocate_hint is true if a.allocate(n, hint) is well-formed.
108#if __cpp_concepts
109 template<typename _Alloc, typename _Sz, typename _Vp>
110 static constexpr bool __has_allocate_hint
111 = requires (_Alloc& __a, _Sz __n, _Vp __hint) {
112 __a.allocate(__n, __hint);
113 };
114#else
115 template<typename _Alloc, typename _Sz, typename _Vp>
116 using __allocate_hint_t
117 = decltype(std::declval<_Alloc&>()
118 .allocate(std::declval<_Sz>(), std::declval<_Vp>()));
119 template<typename _Alloc, typename _Sz, typename _Vp, typename = void>
120 static constexpr bool __has_allocate_hint = false;
121 template<typename _Alloc, typename _Sz, typename _Vp>
122 static constexpr bool
123 __has_allocate_hint<_Alloc, _Sz, _Vp,
124 __void_t<__allocate_hint_t<_Alloc, _Sz, _Vp>>>
125 = true;
126#endif
127
128 // __has_construct is true if a.construct(p, args...) is well-formed.
129 // __can_construct is true if either __has_construct is true, or if
130 // a placement new-expression for T(args...) is well-formed. We use this
131 // to constrain allocator_traits::construct, as a libstdc++ extension.
132#if __cpp_concepts
133 template<typename _Alloc, typename _Tp, typename... _Args>
134 static constexpr bool __has_construct
135 = requires (_Alloc& __a, _Tp* __p, _Args&&... __args) {
136 __a.construct(__p, std::forward<_Args>(__args)...);
137 };
138 template<typename _Tp, typename... _Args>
139 static constexpr bool __can_construct_at
140 = requires (_Tp* __p, _Args&&... __args) {
141#if __cpp_constexpr_dynamic_alloc
142 std::construct_at(__p, std::forward<_Args>(__args)...);
143#else
144 ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
145#endif
146 };
147 template<typename _Alloc, typename _Tp, typename... _Args>
148 static constexpr bool __can_construct
149 = __has_construct<_Alloc, _Tp, _Args...>
150 || __can_construct_at<_Tp, _Args...>;
151#else
152 template<typename _Alloc, typename _Tp, typename... _Args>
153 using __construct_t
154 = decltype(std::declval<_Alloc&>().construct(std::declval<_Tp*>(),
155 std::declval<_Args>()...));
156 template<typename _Alloc, typename _Tp, typename, typename... _Args>
157 static constexpr bool __has_construct_impl = false;
158 template<typename _Alloc, typename _Tp, typename... _Args>
159 static constexpr bool
160 __has_construct_impl<_Alloc, _Tp,
161 __void_t<__construct_t<_Alloc, _Tp, _Args...>>,
162 _Args...>
163 = true;
164 template<typename _Alloc, typename _Tp, typename... _Args>
165 static constexpr bool __has_construct
166 = __has_construct_impl<_Alloc, _Tp, void, _Args...>;
167 template<typename _Tp, typename... _Args>
168 using __new_expr_t
169 = decltype(::new((void*)0) _Tp(std::declval<_Args>()...));
170 template<typename _Tp, typename, typename... _Args>
171 static constexpr bool __has_new_expr = false;
172 template<typename _Tp, typename... _Args>
173 static constexpr bool
174 __has_new_expr<_Tp, __void_t<__new_expr_t<_Tp, _Args...>>, _Args...>
175 = true;
176 template<typename _Alloc, typename _Tp, typename... _Args>
177 static constexpr bool __can_construct
178 = __has_construct<_Alloc, _Tp, _Args...>
179 || __has_new_expr<_Tp, void, _Args...>;
180#endif
181
182 // __has_destroy is true if a.destroy(p) is well-formed.
183#if __cpp_concepts
184 template<typename _Alloc, typename _Tp>
185 static constexpr bool __has_destroy = requires (_Alloc& __a, _Tp* __p) {
186 __a.destroy(__p);
187 };
188#else
189 template<typename _Alloc, typename _Tp>
190 using __destroy_t
191 = decltype(std::declval<_Alloc&>().destroy(std::declval<_Tp*>()));
192 template<typename _Alloc, typename _Tp, typename = void>
193 static constexpr bool __has_destroy = false;
194 template<typename _Alloc, typename _Tp>
195 static constexpr bool __has_destroy<_Alloc, _Tp,
196 __void_t<__destroy_t<_Alloc, _Tp>>>
197 = true;
198#endif
199
200 // __has_max_size is true if a.max_size() is well-formed.
201#if __cpp_concepts
202 template<typename _Alloc>
203 static constexpr bool __has_max_size = requires (const _Alloc& __a) {
204 __a.max_size();
205 };
206#else
207 template<typename _Alloc>
208 using __max_size_t = decltype(std::declval<const _Alloc&>().max_size());
209 template<typename _Alloc, typename = void>
210 static constexpr bool __has_max_size = false;
211 template<typename _Alloc>
212 static constexpr bool __has_max_size<_Alloc,
213 __void_t<__max_size_t<_Alloc>>>
214 = true;
215#endif
216
217 // __has_soccc is true if a.select_on_container_copy_construction()
218 // is well-formed.
219#if __cpp_concepts
220 template<typename _Alloc>
221 static constexpr bool __has_soccc = requires (const _Alloc& __a) {
222 __a.select_on_container_copy_construction();
223 };
224#else
225 template<typename _Alloc>
226 using __soccc_t
227 = decltype(std::declval<const _Alloc&>()
228 .select_on_container_copy_construction());
229 template<typename _Alloc, typename = void>
230 static constexpr bool __has_soccc = false;
231 template<typename _Alloc>
232 static constexpr bool __has_soccc<_Alloc, __void_t<__soccc_t<_Alloc>>>
233 = true;
234#endif
235 };
236
237 template<typename _Alloc, typename _Up>
238 using __alloc_rebind
239 = typename __allocator_traits_base::template __rebind<_Alloc, _Up>::type;
240 /// @endcond
241
242 /**
243 * @brief Uniform interface to all allocator types.
244 * @headerfile memory
245 * @ingroup allocators
246 * @since C++11
247 */
248 template<typename _Alloc>
249 struct allocator_traits : __allocator_traits_base
250 {
251 /// The allocator type
252 typedef _Alloc allocator_type;
253 /// The allocated type
254 typedef typename _Alloc::value_type value_type;
255
256 /**
257 * @brief The allocator's pointer type.
258 *
259 * @c Alloc::pointer if that type exists, otherwise @c value_type*
260 */
261 using pointer = __detected_or_t<value_type*, __pointer, _Alloc>;
262
263 private:
264 // Select _Func<_Alloc> or pointer_traits<pointer>::rebind<_Tp>
265 template<template<typename> class _Func, typename _Tp, typename = void>
266 struct _Ptr
267 {
268 using type = typename pointer_traits<pointer>::template rebind<_Tp>;
269 };
270
271 template<template<typename> class _Func, typename _Tp>
272 struct _Ptr<_Func, _Tp, __void_t<_Func<_Alloc>>>
273 {
274 using type = _Func<_Alloc>;
275 };
276
277 // Select _A2::difference_type or pointer_traits<_Ptr>::difference_type
278 template<typename _A2, typename _PtrT, typename = void>
279 struct _Diff
280 { using type = typename pointer_traits<_PtrT>::difference_type; };
281
282 template<typename _A2, typename _PtrT>
283 struct _Diff<_A2, _PtrT, __void_t<typename _A2::difference_type>>
284 { using type = typename _A2::difference_type; };
285
286 // Select _A2::size_type or make_unsigned<_DiffT>::type
287 template<typename _A2, typename _DiffT, typename = void>
288 struct _Size : make_unsigned<_DiffT> { };
289
290 template<typename _A2, typename _DiffT>
291 struct _Size<_A2, _DiffT, __void_t<typename _A2::size_type>>
292 { using type = typename _A2::size_type; };
293
294 public:
295 /**
296 * @brief The allocator's const pointer type.
297 *
298 * @c Alloc::const_pointer if that type exists, otherwise
299 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
300 */
301 using const_pointer = typename _Ptr<__c_pointer, const value_type>::type;
302
303 /**
304 * @brief The allocator's void pointer type.
305 *
306 * @c Alloc::void_pointer if that type exists, otherwise
307 * <tt> pointer_traits<pointer>::rebind<void> </tt>
308 */
309 using void_pointer = typename _Ptr<__v_pointer, void>::type;
310
311 /**
312 * @brief The allocator's const void pointer type.
313 *
314 * @c Alloc::const_void_pointer if that type exists, otherwise
315 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
316 */
317 using const_void_pointer = typename _Ptr<__cv_pointer, const void>::type;
318
319 /**
320 * @brief The allocator's difference type
321 *
322 * @c Alloc::difference_type if that type exists, otherwise
323 * <tt> pointer_traits<pointer>::difference_type </tt>
324 */
325 using difference_type = typename _Diff<_Alloc, pointer>::type;
326
327 /**
328 * @brief The allocator's size type
329 *
330 * @c Alloc::size_type if that type exists, otherwise
331 * <tt> make_unsigned<difference_type>::type </tt>
332 */
333 using size_type = typename _Size<_Alloc, difference_type>::type;
334
335 /**
336 * @brief How the allocator is propagated on copy assignment
337 *
338 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
339 * otherwise @c false_type
340 */
342 = __detected_or_t<false_type, __pocca, _Alloc>;
343
344 /**
345 * @brief How the allocator is propagated on move assignment
346 *
347 * @c Alloc::propagate_on_container_move_assignment if that type exists,
348 * otherwise @c false_type
349 */
351 = __detected_or_t<false_type, __pocma, _Alloc>;
352
353 /**
354 * @brief How the allocator is propagated on swap
355 *
356 * @c Alloc::propagate_on_container_swap if that type exists,
357 * otherwise @c false_type
358 */
360 = __detected_or_t<false_type, __pocs, _Alloc>;
361
362 /**
363 * @brief Whether all instances of the allocator type compare equal.
364 *
365 * @c Alloc::is_always_equal if that type exists,
366 * otherwise @c is_empty<Alloc>::type
367 */
368 using is_always_equal
369 = typename __detected_or_t<is_empty<_Alloc>, __equal, _Alloc>::type;
370
371 template<typename _Tp>
372 using rebind_alloc = __alloc_rebind<_Alloc, _Tp>;
373 template<typename _Tp>
374 using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
375
376 /**
377 * @brief Allocate memory.
378 * @param __a An allocator.
379 * @param __n The number of objects to allocate space for.
381 * Calls @c a.allocate(n)
382 */
383 _GLIBCXX_NODISCARD static _GLIBCXX20_CONSTEXPR pointer
384 allocate(_Alloc& __a, size_type __n)
385 { return __a.allocate(__n); }
386
387 /**
388 * @brief Allocate memory.
389 * @param __a An allocator.
390 * @param __n The number of objects to allocate space for.
391 * @param __hint Aid to locality.
392 * @return Memory of suitable size and alignment for @a n objects
393 * of type @c value_type
394 *
395 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
396 * well-formed, otherwise returns @c a.allocate(n)
397 */
398 _GLIBCXX_NODISCARD static _GLIBCXX20_CONSTEXPR pointer
399 allocate(_Alloc& __a, size_type __n, const_void_pointer __hint)
400 {
401 if constexpr (__has_allocate_hint<_Alloc, size_type, const_void_pointer>)
402 return __a.allocate(__n, __hint);
403 else
404 return __a.allocate(__n);
405 }
406
407#ifdef __glibcxx_allocate_at_least // C++23
408 /**
409 * @brief Allocate memory, generously.
410 * @param __a An allocator.
411 * @param __n The minimum number of objects to allocate space for.
412 * @return Memory of suitable size and alignment for `n` or more
413 * contiguous objects of type `value_type`.
414 *
415 * Returns `a.allocate_at_least(n)` if that expression is
416 * well-formed, else `{ a.allocate(n), n }`. When an allocator
417 * is obliged to reserve more space than required for the cited
418 * `n` objects, it may deliver the extra space to the caller.
419 */
420 [[nodiscard]] static constexpr allocation_result<pointer, size_type>
421 allocate_at_least(_Alloc& __a, size_type __n)
422 {
423 if constexpr (requires { __a.allocate_at_least(__n); })
424 return __a.allocate_at_least(__n);
425 else
426 return { __a.allocate(__n), __n };
427 }
428#endif
429
430 /**
431 * @brief Deallocate memory.
432 * @param __a An allocator.
433 * @param __p Pointer to the memory to deallocate.
434 * @param __n The number of objects space was allocated for.
436 * Calls <tt> a.deallocate(p, n) </tt>
437 */
438 static _GLIBCXX20_CONSTEXPR void
439 deallocate(_Alloc& __a, pointer __p, size_type __n)
440 { __a.deallocate(__p, __n); }
441
442 /**
443 * @brief Construct an object of type `_Tp`
444 * @param __a An allocator.
445 * @param __p Pointer to memory of suitable size and alignment for Tp
446 * @param __args Constructor arguments.
447 *
448 * Calls <tt> __a.construct(__p, std::forward<Args>(__args)...) </tt>
449 * if that expression is well-formed, otherwise uses placement-new
450 * to construct an object of type @a _Tp at location @a __p from the
451 * arguments @a __args...
452 */
453 template<typename _Tp, typename... _Args>
454#if __cpp_concepts && __cpp_constexpr_dynamic_alloc
455 requires __can_construct<_Alloc, _Tp, _Args...>
456 static constexpr void
457#else
458 static __enable_if_t<__can_construct<_Alloc, _Tp, _Args...>>
459#endif
460 construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
461 noexcept(_S_nothrow_construct<_Tp, _Args...>())
462 {
463 if constexpr (__has_construct<_Alloc, _Tp, _Args...>)
464 __a.construct(__p, std::forward<_Args>(__args)...);
465 else
466 std::_Construct(__p, std::forward<_Args>(__args)...);
467 }
468
469 /**
470 * @brief Destroy an object of type @a _Tp
471 * @param __a An allocator.
472 * @param __p Pointer to the object to destroy
473 *
474 * Calls @c __a.destroy(__p) if that expression is well-formed,
475 * otherwise calls @c __p->~_Tp()
476 */
477 template<typename _Tp>
478 static _GLIBCXX20_CONSTEXPR void
479 destroy(_Alloc& __a, _Tp* __p)
480 noexcept(_S_nothrow_destroy<_Tp>())
481 {
482 if constexpr (__has_destroy<_Alloc, _Tp>)
483 __a.destroy(__p);
484 else
485 std::_Destroy(__p);
486 }
487
488 /**
489 * @brief The maximum supported allocation size
490 * @param __a An allocator.
491 * @return @c __a.max_size() or @c numeric_limits<size_type>::max()
492 *
493 * Returns @c __a.max_size() if that expression is well-formed,
494 * otherwise returns @c numeric_limits<size_type>::max()
495 */
496 static _GLIBCXX20_CONSTEXPR size_type
497 max_size(const _Alloc& __a) noexcept
498 {
499 if constexpr (__has_max_size<_Alloc>)
500 return __a.max_size();
501 else
502 // _GLIBCXX_RESOLVE_LIB_DEFECTS
503 // 2466. allocator_traits::max_size() default behavior is incorrect
504 return __gnu_cxx::__numeric_traits<size_type>::__max
505 / sizeof(value_type);
506 }
507
508 /**
509 * @brief Obtain an allocator to use when copying a container.
510 * @param __rhs An allocator.
511 * @return @c __rhs.select_on_container_copy_construction() or @a __rhs
512 *
513 * Returns @c __rhs.select_on_container_copy_construction() if that
514 * expression is well-formed, otherwise returns @a __rhs
515 */
516 static _GLIBCXX20_CONSTEXPR _Alloc
517 select_on_container_copy_construction(const _Alloc& __rhs)
518 {
519 if constexpr (__has_soccc<_Alloc>)
520 return __rhs.select_on_container_copy_construction();
521 else
522 return __rhs;
523 }
524
525 private:
526#if __cpp_constexpr >= 201304 // >= C++14
527 template<typename _Tp, typename... _Args>
528 static constexpr bool
529 _S_nothrow_construct(_Alloc* __a = nullptr, _Tp* __p = nullptr)
530 {
531 if constexpr (__has_construct<_Alloc, _Tp, _Args...>)
532 return noexcept(__a->construct(__p, std::declval<_Args>()...));
533 else
534 return __is_nothrow_new_constructible<_Tp, _Args...>;
535 }
536
537 template<typename _Tp>
538 static constexpr bool
539 _S_nothrow_destroy(_Alloc* __a = nullptr, _Tp* __p = nullptr)
540 {
541 if constexpr (__has_destroy<_Alloc, _Tp>)
542 return noexcept(__a->destroy(__p));
543 else
544 return is_nothrow_destructible<_Tp>::value;
545 }
546#else
547 template<typename _Tp, typename... _Args>
548 static constexpr
549 __enable_if_t<__has_construct<_Alloc, _Tp, _Args...>, bool>
550 _S_nothrow_construct(_Alloc* __a = nullptr, _Tp* __p = nullptr)
551 { return noexcept(__a->construct(__p, std::declval<_Args>()...)); }
552
553 template<typename _Tp, typename... _Args>
554 static constexpr
555 __enable_if_t<!__has_construct<_Alloc, _Tp, _Args...>, bool>
556 _S_nothrow_construct(_Alloc* = nullptr, _Tp* __p = nullptr)
557 { return __is_nothrow_new_constructible<_Tp, _Args...>; }
558
559 template<typename _Tp>
560 static constexpr
561 __enable_if_t<__has_destroy<_Alloc, _Tp>, bool>
562 _S_nothrow_destroy(_Alloc* __a = nullptr, _Tp* __p = nullptr)
563 { return noexcept(__a->destroy(__p)); }
564
565 template<typename _Tp>
566 static constexpr
567 __enable_if_t<!__has_destroy<_Alloc, _Tp>, bool>
568 _S_nothrow_destroy(_Alloc* = nullptr, _Tp* __p = nullptr)
569 { return is_nothrow_destructible<_Tp>::value; }
570#endif
571 };
572#pragma GCC diagnostic pop
573
574#if _GLIBCXX_HOSTED
575 /**
576 * @brief Partial specialization for `std::allocator`
577 * @headerfile memory
578 * @ingroup allocators
579 * @since C++11
580 * @see std::allocator_traits
581 */
582 template<typename _Tp>
584 {
585 /// The allocator type
587
588 /// The allocated type
589 using value_type = _Tp;
590
591 /// The allocator's pointer type.
592 using pointer = _Tp*;
593
594 /// The allocator's const pointer type.
595 using const_pointer = const _Tp*;
596
597 /// The allocator's void pointer type.
598 using void_pointer = void*;
599
600 /// The allocator's const void pointer type.
601 using const_void_pointer = const void*;
602
603 /// The allocator's difference type
604 using difference_type = std::ptrdiff_t;
605
606 /// The allocator's size type
607 using size_type = std::size_t;
608
609 /// How the allocator is propagated on copy assignment
611
612 /// How the allocator is propagated on move assignment
614
615 /// How the allocator is propagated on swap
617
618 /// Whether all instances of the allocator type compare equal.
620
621 template<typename _Up>
622 using rebind_alloc = allocator<_Up>;
623
624 template<typename _Up>
625 using rebind_traits = allocator_traits<allocator<_Up>>;
626
627 /**
628 * @brief Allocate memory.
629 * @param __a An allocator.
630 * @param __n The number of objects to allocate space for.
631 *
632 * Calls @c a.allocate(n)
633 */
634 [[__nodiscard__,__gnu__::__always_inline__]]
635 static _GLIBCXX20_CONSTEXPR pointer
637 { return __a.allocate(__n); }
638
639 /**
640 * @brief Allocate memory.
641 * @param __a An allocator.
642 * @param __n The number of objects to allocate space for.
643 * @param __hint Aid to locality.
644 * @return Memory of suitable size and alignment for @a n objects
645 * of type @c value_type
646 *
647 * Returns <tt> a.allocate(n, hint) </tt>
648 */
649 [[__nodiscard__,__gnu__::__always_inline__]]
650 static _GLIBCXX20_CONSTEXPR pointer
652 [[maybe_unused]] const_void_pointer __hint)
653 {
654#if __cplusplus <= 201703L
655 return __a.allocate(__n, __hint);
656#else
657 return __a.allocate(__n);
658#endif
659 }
660
661#ifdef __glibcxx_allocate_at_least // C++23
662 /**
663 * @brief Allocate memory, generously.
664 * @param __a An allocator.
665 * @param __n The minimum number of objects to allocate space for.
666 * @return Memory of suitable size and alignment for `m >= n`
667 * contiguous objects of type `value_type`, and `m`.
668 *
669 * Returns `a.allocate_at_least(n)`.
670 */
671 [[nodiscard,__gnu__::__always_inline__]]
672 static constexpr allocation_result<pointer, size_type>
674 { return __a.allocate_at_least(__n); }
675#endif
676
677 /**
678 * @brief Deallocate memory.
679 * @param __a An allocator.
680 * @param __p Pointer to the memory to deallocate.
681 * @param __n The number of objects space was allocated for.
682 *
683 * Calls <tt> a.deallocate(p, n) </tt>
684 */
685 [[__gnu__::__always_inline__]]
686 static _GLIBCXX20_CONSTEXPR void
688 { __a.deallocate(__p, __n); }
689
690 /**
691 * @brief Construct an object of type `_Up`
692 * @param __a An allocator.
693 * @param __p Pointer to memory of suitable size and alignment for
694 * an object of type `_Up`.
695 * @param __args Constructor arguments.
696 *
697 * Calls `__a.construct(__p, std::forward<_Args>(__args)...)`
698 * in C++11, C++14 and C++17. Changed in C++20 to call
699 * `std::construct_at(__p, std::forward<_Args>(__args)...)` instead.
700 */
701 template<typename _Up, typename... _Args>
702 [[__gnu__::__always_inline__]]
703 static _GLIBCXX20_CONSTEXPR void
704 construct(allocator_type& __a __attribute__((__unused__)),
705 _Up* __p, _Args&&... __args)
706#if __cplusplus <= 201703L
707 noexcept(noexcept(__a.construct(__p, std::forward<_Args>(__args)...)))
708#else
709 noexcept(__is_nothrow_new_constructible<_Up, _Args...>)
710#endif
711 {
712#if __cplusplus <= 201703L
713 __a.construct(__p, std::forward<_Args>(__args)...);
714#elif __cpp_constexpr_dynamic_alloc // >= C++20
715 std::construct_at(__p, std::forward<_Args>(__args)...);
716#else
717 std::_Construct(__p, std::forward<_Args>(__args)...);
718#endif
719 }
720
721 /**
722 * @brief Destroy an object of type @a _Up
723 * @param __a An allocator.
724 * @param __p Pointer to the object to destroy
725 *
726 * Calls @c __a.destroy(__p).
727 */
728 template<typename _Up>
729 [[__gnu__::__always_inline__]]
730 static _GLIBCXX20_CONSTEXPR void
731 destroy(allocator_type& __a __attribute__((__unused__)), _Up* __p)
733 {
734#if __cplusplus <= 201703L
735 __a.destroy(__p);
736#else
737 std::destroy_at(__p);
738#endif
739 }
740
741 /**
742 * @brief The maximum supported allocation size
743 * @param __a An allocator.
744 * @return @c __a.max_size()
745 */
746 [[__gnu__::__always_inline__]]
747 static _GLIBCXX20_CONSTEXPR size_type
748 max_size(const allocator_type& __a __attribute__((__unused__))) noexcept
749 {
750#if __cplusplus <= 201703L
751 return __a.max_size();
752#else
753 return size_t(-1) / sizeof(value_type);
754#endif
755 }
756
757 /**
758 * @brief Obtain an allocator to use when copying a container.
759 * @param __rhs An allocator.
760 * @return @c __rhs
761 */
762 [[__gnu__::__always_inline__]]
763 static _GLIBCXX20_CONSTEXPR allocator_type
765 { return __rhs; }
766 };
767
768 /**
769 * @brief Explicit specialization for `std::allocator<void>`
770 * @headerfile memory
771 * @ingroup allocators
772 * @since C++11
773 * @see std::allocator_traits
774 */
775 template<>
777 {
778 /// The allocator type
780
781 /// The allocated type
782 using value_type = void;
783
784 /// The allocator's pointer type.
785 using pointer = void*;
786
787 /// The allocator's const pointer type.
788 using const_pointer = const void*;
789
790 /// The allocator's void pointer type.
791 using void_pointer = void*;
792
793 /// The allocator's const void pointer type.
794 using const_void_pointer = const void*;
795
796 /// The allocator's difference type
797 using difference_type = std::ptrdiff_t;
798
799 /// The allocator's size type
800 using size_type = std::size_t;
801
802 /// How the allocator is propagated on copy assignment
804
805 /// How the allocator is propagated on move assignment
807
808 /// How the allocator is propagated on swap
810
811 /// Whether all instances of the allocator type compare equal.
813
814 template<typename _Up>
815 using rebind_alloc = allocator<_Up>;
816
817 template<typename _Up>
818 using rebind_traits = allocator_traits<allocator<_Up>>;
819
820 /// allocate is ill-formed for allocator<void>
821 static void*
822 allocate(allocator_type&, size_type, const void* = nullptr) = delete;
823
824#ifdef __glibcxx_allocate_at_least
825 static allocation_result<void*, size_type>
827#endif
828
829 /// deallocate is ill-formed for allocator<void>
830 static void
832
833 /**
834 * @brief Construct an object of type `_Up`
835 * @param __a An allocator.
836 * @param __p Pointer to memory of suitable size and alignment for
837 * an object of type `_Up`.
838 * @param __args Constructor arguments.
839 *
840 * Calls `__a.construct(__p, std::forward<_Args>(__args)...)`
841 * in C++11, C++14 and C++17. Changed in C++20 to call
842 * `std::construct_at(__p, std::forward<_Args>(__args)...)` instead.
843 */
844 template<typename _Up, typename... _Args>
845 [[__gnu__::__always_inline__]]
846 static _GLIBCXX20_CONSTEXPR void
847 construct(allocator_type&, _Up* __p, _Args&&... __args)
848 noexcept(__is_nothrow_new_constructible<_Up, _Args...>)
849 { std::_Construct(__p, std::forward<_Args>(__args)...); }
850
851 /**
852 * @brief Destroy an object of type `_Up`
853 * @param __a An allocator.
854 * @param __p Pointer to the object to destroy
855 *
856 * Invokes the destructor for `*__p`.
857 */
858 template<typename _Up>
859 [[__gnu__::__always_inline__]]
860 static _GLIBCXX20_CONSTEXPR void
864
865 /// max_size is ill-formed for allocator<void>
866 static size_type
867 max_size(const allocator_type&) = delete;
868
869 /**
870 * @brief Obtain an allocator to use when copying a container.
871 * @param __rhs An allocator.
872 * @return `__rhs`
873 */
874 [[__gnu__::__always_inline__]]
875 static _GLIBCXX20_CONSTEXPR allocator_type
877 { return __rhs; }
878 };
879#endif // _GLIBCXX_HOSTED
880
881 /// @cond undocumented
882#pragma GCC diagnostic push
883#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
884 template<typename _Alloc>
885 [[__gnu__::__always_inline__]]
886 _GLIBCXX14_CONSTEXPR inline void
887 __alloc_on_copy(_Alloc& __one, const _Alloc& __two)
888 {
889 using __traits = allocator_traits<_Alloc>;
890 using __pocca =
891 typename __traits::propagate_on_container_copy_assignment::type;
892 if constexpr (__pocca::value)
893 __one = __two;
894 }
895
896 template<typename _Alloc>
897 [[__gnu__::__always_inline__]]
898 constexpr _Alloc
899 __alloc_on_copy(const _Alloc& __a)
900 {
901 typedef allocator_traits<_Alloc> __traits;
902 return __traits::select_on_container_copy_construction(__a);
903 }
904
905 template<typename _Alloc>
906 [[__gnu__::__always_inline__]]
907 _GLIBCXX14_CONSTEXPR inline void
908 __alloc_on_move(_Alloc& __one, _Alloc& __two)
909 {
910 using __traits = allocator_traits<_Alloc>;
911 using __pocma
912 = typename __traits::propagate_on_container_move_assignment::type;
913 if constexpr (__pocma::value)
914 __one = std::move(__two);
915 }
916
917 template<typename _Alloc>
918 [[__gnu__::__always_inline__]]
919 _GLIBCXX14_CONSTEXPR inline void
920 __alloc_on_swap(_Alloc& __one, _Alloc& __two)
921 {
922 using __traits = allocator_traits<_Alloc>;
923 using __pocs = typename __traits::propagate_on_container_swap::type;
924 if constexpr (__pocs::value)
925 {
926 using std::swap;
927 swap(__one, __two);
928 }
929 }
930#pragma GCC diagnostic pop
931
932 template<typename _Alloc, typename _Tp,
933 typename _ValueT = __remove_cvref_t<typename _Alloc::value_type>,
934 typename = void>
935 struct __is_alloc_insertable_impl
936 : false_type
937 { };
938
939 template<typename _Alloc, typename _Tp, typename _ValueT>
940 struct __is_alloc_insertable_impl<_Alloc, _Tp, _ValueT,
941 __void_t<decltype(allocator_traits<_Alloc>::construct(
942 std::declval<_Alloc&>(), std::declval<_ValueT*>(),
943 std::declval<_Tp>()))>>
944 : true_type
945 { };
946
947 // true if _Alloc::value_type is CopyInsertable into containers using _Alloc
948 // (might be wrong if _Alloc::construct exists but is not constrained,
949 // i.e. actually trying to use it would still be invalid. Use with caution.)
950 template<typename _Alloc>
951 struct __is_copy_insertable
952 : __is_alloc_insertable_impl<_Alloc,
953 typename _Alloc::value_type const&>::type
954 { };
955
956#if _GLIBCXX_HOSTED
957 // std::allocator<_Tp> just requires CopyConstructible
958 template<typename _Tp>
959 struct __is_copy_insertable<allocator<_Tp>>
961 { };
962#endif
963
964 // true if _Alloc::value_type is MoveInsertable into containers using _Alloc
965 // (might be wrong if _Alloc::construct exists but is not constrained,
966 // i.e. actually trying to use it would still be invalid. Use with caution.)
967 template<typename _Alloc>
968 struct __is_move_insertable
969 : __is_alloc_insertable_impl<_Alloc, typename _Alloc::value_type>::type
970 { };
971
972#if _GLIBCXX_HOSTED
973 // std::allocator<_Tp> just requires MoveConstructible
974 template<typename _Tp>
975 struct __is_move_insertable<allocator<_Tp>>
977 { };
978#endif
979
980 // Trait to detect Allocator-like types.
981 template<typename _Alloc, typename = void>
982 struct __is_allocator : false_type { };
983
984 template<typename _Alloc>
985 struct __is_allocator<_Alloc,
986 __void_t<typename _Alloc::value_type,
987 decltype(std::declval<_Alloc&>().allocate(size_t{}))>>
988 : true_type { };
989
990 template<typename _Alloc>
991 using _RequireAllocator
992 = typename enable_if<__is_allocator<_Alloc>::value, _Alloc>::type;
993
994 template<typename _Alloc>
995 using _RequireNotAllocator
996 = typename enable_if<!__is_allocator<_Alloc>::value, _Alloc>::type;
997
998#if __cpp_concepts >= 201907L
999 template<typename _Alloc>
1000 concept __allocator_like = requires (_Alloc& __a) {
1001 typename _Alloc::value_type;
1002 __a.deallocate(__a.allocate(1u), 1u);
1003 };
1004
1005 template<typename _Alloc>
1006 concept __not_allocator_like = !__allocator_like<_Alloc>;
1007#endif
1008 /// @endcond
1009#endif // C++11
1010
1011 /// @cond undocumented
1012
1013 // To implement Option 3 of DR 431.
1014 template<typename _Alloc, bool = __is_empty(_Alloc)>
1015 struct __alloc_swap
1016 { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
1017
1018 template<typename _Alloc>
1019 struct __alloc_swap<_Alloc, false>
1020 {
1021 static void
1022 _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
1023 {
1024 // Precondition: swappable allocators.
1025 if (__one != __two)
1026 swap(__one, __two);
1027 }
1028 };
1029
1030#if __cplusplus >= 201103L
1031 template<typename _Tp, bool
1032 = __or_<is_copy_constructible<typename _Tp::value_type>,
1034 struct __shrink_to_fit_aux
1035 { static bool _S_do_it(_Tp&) noexcept { return false; } };
1036
1037 template<typename _Tp>
1038 struct __shrink_to_fit_aux<_Tp, true>
1039 {
1040 _GLIBCXX20_CONSTEXPR
1041 static bool
1042 _S_do_it(_Tp& __c) noexcept
1043 {
1044#if __cpp_exceptions
1045 try
1046 {
1047 _Tp(__make_move_if_noexcept_iterator(__c.begin()),
1048 __make_move_if_noexcept_iterator(__c.end()),
1049 __c.get_allocator()).swap(__c);
1050 return true;
1051 }
1052 catch(...)
1053 { return false; }
1054#else
1055 return false;
1056#endif
1057 }
1058 };
1059#endif
1060
1061 /**
1062 * Destroy a range of objects using the supplied allocator. For
1063 * non-default allocators we do not optimize away invocation of
1064 * destroy() even if _Tp has a trivial destructor.
1065 */
1066
1067 template<typename _ForwardIterator, typename _Allocator>
1068 _GLIBCXX20_CONSTEXPR
1069 void
1070 _Destroy(_ForwardIterator __first, _ForwardIterator __last,
1071 _Allocator& __alloc)
1072 {
1073 for (; __first != __last; ++__first)
1074#if __cplusplus < 201103L
1075 __alloc.destroy(std::__addressof(*__first));
1076#else
1078 std::__addressof(*__first));
1079#endif
1080 }
1081
1082#if _GLIBCXX_HOSTED
1083 template<typename _ForwardIterator, typename _Tp>
1084 __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
1085 inline void
1086 _Destroy(_ForwardIterator __first, _ForwardIterator __last,
1088 {
1089 std::_Destroy(__first, __last);
1090 }
1091#endif
1092
1093 /// @endcond
1094
1095_GLIBCXX_END_NAMESPACE_VERSION
1096} // namespace std
1097#endif // _ALLOC_TRAITS_H
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2741
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
constexpr void _Construct(_Tp *__p, _Args &&... __args)
constexpr void _Destroy(_ForwardIterator __first, _ForwardIterator __last)
Define a member typedef type only if a boolean constant is true.
Definition type_traits:137
is_nothrow_destructible
Definition type_traits:1198
is_copy_constructible
Definition type_traits:1301
is_move_constructible
Definition type_traits:1328
is_nothrow_move_constructible
Definition type_traits:1370
Uniform interface to all allocator types.
__detected_or_t< false_type, __pocma, _OuterAlloc > propagate_on_container_move_assignment
typename _Ptr< __v_pointer, void >::type void_pointer
__detected_or_t< value_type *, __pointer, _OuterAlloc > pointer
static constexpr void construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(_S_nothrow_construct< _Tp, _Args... >())
Construct an object of type _Tp.
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
typename _Size< _Alloc, difference_type >::type size_type
The allocator's size type.
typename _Ptr< __cv_pointer, const void >::type const_void_pointer
static constexpr allocation_result< pointer, size_type > allocate_at_least(_Alloc &__a, size_type __n)
Allocate memory, generously.
static constexpr void destroy(_Alloc &__a, _Tp *__p) noexcept(_S_nothrow_destroy< _Tp >())
Destroy an object of type _Tp.
typename _Diff< _Alloc, pointer >::type difference_type
The allocator's difference type.
typename _Ptr< __c_pointer, const value_type >::type const_pointer
static constexpr void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
typename __detected_or_t< is_empty< _OuterAlloc >, __equal, _OuterAlloc >::type is_always_equal
static constexpr size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.
__detected_or_t< false_type, __pocca, _OuterAlloc > propagate_on_container_copy_assignment
static constexpr _Alloc select_on_container_copy_construction(const _Alloc &__rhs)
Obtain an allocator to use when copying a container.
__detected_or_t< false_type, __pocs, _OuterAlloc > propagate_on_container_swap
allocator< _Tp > allocator_type
The allocator type.
static constexpr void construct(allocator_type &__a, _Up *__p, _Args &&... __args) noexcept(__is_nothrow_new_constructible< _Up, _Args... >)
Construct an object of type _Up.
void * void_pointer
The allocator's void pointer type.
_Tp * pointer
The allocator's pointer type.
false_type propagate_on_container_swap
How the allocator is propagated on swap.
static constexpr pointer allocate(allocator_type &__a, size_type __n)
Allocate memory.
static constexpr pointer allocate(allocator_type &__a, size_type __n, const_void_pointer __hint)
Allocate memory.
std::ptrdiff_t difference_type
The allocator's difference type.
true_type is_always_equal
Whether all instances of the allocator type compare equal.
const _Tp * const_pointer
The allocator's const pointer type.
static constexpr allocation_result< pointer, size_type > allocate_at_least(allocator_type &__a, size_type __n)
Allocate memory, generously.
const void * const_void_pointer
The allocator's const void pointer type.
true_type propagate_on_container_move_assignment
How the allocator is propagated on move assignment.
static constexpr void deallocate(allocator_type &__a, pointer __p, size_type __n)
Deallocate memory.
static constexpr size_type max_size(const allocator_type &__a) noexcept
The maximum supported allocation size.
static constexpr allocator_type select_on_container_copy_construction(const allocator_type &__rhs)
Obtain an allocator to use when copying a container.
static constexpr void destroy(allocator_type &__a, _Up *__p) noexcept(is_nothrow_destructible< _Up >::value)
Destroy an object of type _Up.
false_type propagate_on_container_copy_assignment
How the allocator is propagated on copy assignment.
std::size_t size_type
The allocator's size type.
false_type propagate_on_container_copy_assignment
How the allocator is propagated on copy assignment.
void * pointer
The allocator's pointer type.
void * void_pointer
The allocator's void pointer type.
static void deallocate(allocator_type &, void *, size_type)=delete
deallocate is ill-formed for allocator<void>
allocator< void > allocator_type
The allocator type.
true_type is_always_equal
Whether all instances of the allocator type compare equal.
static size_type max_size(const allocator_type &)=delete
max_size is ill-formed for allocator<void>
std::size_t size_type
The allocator's size type.
true_type propagate_on_container_move_assignment
How the allocator is propagated on move assignment.
const void * const_pointer
The allocator's const pointer type.
std::ptrdiff_t difference_type
The allocator's difference type.
static void * allocate(allocator_type &, size_type, const void *=nullptr)=delete
allocate is ill-formed for allocator<void>
static constexpr allocator_type select_on_container_copy_construction(const allocator_type &__rhs)
Obtain an allocator to use when copying a container.
static constexpr void construct(allocator_type &, _Up *__p, _Args &&... __args) noexcept(__is_nothrow_new_constructible< _Up, _Args... >)
Construct an object of type _Up.
const void * const_void_pointer
The allocator's const void pointer type.
static constexpr void destroy(allocator_type &, _Up *__p) noexcept(is_nothrow_destructible< _Up >::value)
Destroy an object of type _Up.
false_type propagate_on_container_swap
How the allocator is propagated on swap.
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
Uniform interface to all pointer-like types.
Definition ptr_traits.h:180