libstdc++
bits/alloc_traits.h
Go to the documentation of this file.
1// Allocator traits -*- C++ -*-
2
3// Copyright (C) 2011-2024 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#endif
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47#if __cplusplus >= 201103L
48 /// @cond undocumented
49 struct __allocator_traits_base
50 {
51 template<typename _Tp, typename _Up, typename = void>
52 struct __rebind : __replace_first_arg<_Tp, _Up>
53 {
54 static_assert(is_same<
55 typename __replace_first_arg<_Tp, typename _Tp::value_type>::type,
56 _Tp>::value,
57 "allocator_traits<A>::rebind_alloc<A::value_type> must be A");
58 };
59
60 template<typename _Tp, typename _Up>
61 struct __rebind<_Tp, _Up,
62 __void_t<typename _Tp::template rebind<_Up>::other>>
63 {
64 using type = typename _Tp::template rebind<_Up>::other;
66 static_assert(is_same<
67 typename _Tp::template rebind<typename _Tp::value_type>::other,
68 _Tp>::value,
69 "allocator_traits<A>::rebind_alloc<A::value_type> must be A");
70 };
71
72 protected:
73 template<typename _Tp>
74 using __pointer = typename _Tp::pointer;
75 template<typename _Tp>
76 using __c_pointer = typename _Tp::const_pointer;
77 template<typename _Tp>
78 using __v_pointer = typename _Tp::void_pointer;
79 template<typename _Tp>
80 using __cv_pointer = typename _Tp::const_void_pointer;
81 template<typename _Tp>
82 using __pocca = typename _Tp::propagate_on_container_copy_assignment;
83 template<typename _Tp>
84 using __pocma = typename _Tp::propagate_on_container_move_assignment;
85 template<typename _Tp>
86 using __pocs = typename _Tp::propagate_on_container_swap;
87 template<typename _Tp>
88 using __equal = __type_identity<typename _Tp::is_always_equal>;
89 };
90
91 template<typename _Alloc, typename _Up>
92 using __alloc_rebind
93 = typename __allocator_traits_base::template __rebind<_Alloc, _Up>::type;
94 /// @endcond
95
96 /**
97 * @brief Uniform interface to all allocator types.
98 * @headerfile memory
99 * @ingroup allocators
100 * @since C++11
101 */
102 template<typename _Alloc>
103 struct allocator_traits : __allocator_traits_base
104 {
105 /// The allocator type
106 typedef _Alloc allocator_type;
107 /// The allocated type
108 typedef typename _Alloc::value_type value_type;
109
110 /**
111 * @brief The allocator's pointer type.
112 *
113 * @c Alloc::pointer if that type exists, otherwise @c value_type*
114 */
115 using pointer = __detected_or_t<value_type*, __pointer, _Alloc>;
116
117 private:
118 // Select _Func<_Alloc> or pointer_traits<pointer>::rebind<_Tp>
119 template<template<typename> class _Func, typename _Tp, typename = void>
120 struct _Ptr
121 {
122 using type = typename pointer_traits<pointer>::template rebind<_Tp>;
123 };
124
125 template<template<typename> class _Func, typename _Tp>
126 struct _Ptr<_Func, _Tp, __void_t<_Func<_Alloc>>>
127 {
128 using type = _Func<_Alloc>;
129 };
130
131 // Select _A2::difference_type or pointer_traits<_Ptr>::difference_type
132 template<typename _A2, typename _PtrT, typename = void>
133 struct _Diff
134 { using type = typename pointer_traits<_PtrT>::difference_type; };
135
136 template<typename _A2, typename _PtrT>
137 struct _Diff<_A2, _PtrT, __void_t<typename _A2::difference_type>>
138 { using type = typename _A2::difference_type; };
139
140 // Select _A2::size_type or make_unsigned<_DiffT>::type
141 template<typename _A2, typename _DiffT, typename = void>
142 struct _Size : make_unsigned<_DiffT> { };
143
144 template<typename _A2, typename _DiffT>
145 struct _Size<_A2, _DiffT, __void_t<typename _A2::size_type>>
146 { using type = typename _A2::size_type; };
147
148 public:
149 /**
150 * @brief The allocator's const pointer type.
151 *
152 * @c Alloc::const_pointer if that type exists, otherwise
153 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
154 */
155 using const_pointer = typename _Ptr<__c_pointer, const value_type>::type;
156
157 /**
158 * @brief The allocator's void pointer type.
159 *
160 * @c Alloc::void_pointer if that type exists, otherwise
161 * <tt> pointer_traits<pointer>::rebind<void> </tt>
162 */
163 using void_pointer = typename _Ptr<__v_pointer, void>::type;
164
165 /**
166 * @brief The allocator's const void pointer type.
167 *
168 * @c Alloc::const_void_pointer if that type exists, otherwise
169 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
170 */
171 using const_void_pointer = typename _Ptr<__cv_pointer, const void>::type;
172
173 /**
174 * @brief The allocator's difference type
175 *
176 * @c Alloc::difference_type if that type exists, otherwise
177 * <tt> pointer_traits<pointer>::difference_type </tt>
178 */
179 using difference_type = typename _Diff<_Alloc, pointer>::type;
180
181 /**
182 * @brief The allocator's size type
183 *
184 * @c Alloc::size_type if that type exists, otherwise
185 * <tt> make_unsigned<difference_type>::type </tt>
186 */
187 using size_type = typename _Size<_Alloc, difference_type>::type;
188
189 /**
190 * @brief How the allocator is propagated on copy assignment
191 *
192 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
193 * otherwise @c false_type
194 */
196 = __detected_or_t<false_type, __pocca, _Alloc>;
197
198 /**
199 * @brief How the allocator is propagated on move assignment
200 *
201 * @c Alloc::propagate_on_container_move_assignment if that type exists,
202 * otherwise @c false_type
203 */
205 = __detected_or_t<false_type, __pocma, _Alloc>;
206
207 /**
208 * @brief How the allocator is propagated on swap
209 *
210 * @c Alloc::propagate_on_container_swap if that type exists,
211 * otherwise @c false_type
212 */
214 = __detected_or_t<false_type, __pocs, _Alloc>;
215
216 /**
217 * @brief Whether all instances of the allocator type compare equal.
218 *
219 * @c Alloc::is_always_equal if that type exists,
220 * otherwise @c is_empty<Alloc>::type
221 */
223 = typename __detected_or_t<is_empty<_Alloc>, __equal, _Alloc>::type;
224
225 template<typename _Tp>
226 using rebind_alloc = __alloc_rebind<_Alloc, _Tp>;
227 template<typename _Tp>
228 using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
229
230 private:
231 template<typename _Alloc2>
232 static constexpr auto
233 _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer __hint, int)
234 -> decltype(__a.allocate(__n, __hint))
235 { return __a.allocate(__n, __hint); }
236
237 template<typename _Alloc2>
238 static constexpr pointer
239 _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer, ...)
240 { return __a.allocate(__n); }
241
242 template<typename _Tp, typename... _Args>
243 struct __construct_helper
244 {
245 template<typename _Alloc2,
246 typename = decltype(std::declval<_Alloc2*>()->construct(
247 std::declval<_Tp*>(), std::declval<_Args>()...))>
248 static true_type __test(int);
249
250 template<typename>
251 static false_type __test(...);
252
253 using type = decltype(__test<_Alloc>(0));
254 };
255
256 template<typename _Tp, typename... _Args>
257 using __has_construct
258 = typename __construct_helper<_Tp, _Args...>::type;
259
260 template<typename _Tp, typename... _Args>
261 static _GLIBCXX14_CONSTEXPR _Require<__has_construct<_Tp, _Args...>>
262 _S_construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
263 noexcept(noexcept(__a.construct(__p, std::forward<_Args>(__args)...)))
264 { __a.construct(__p, std::forward<_Args>(__args)...); }
265
266 template<typename _Tp, typename... _Args>
267 static _GLIBCXX14_CONSTEXPR
268 _Require<__and_<__not_<__has_construct<_Tp, _Args...>>,
269 is_constructible<_Tp, _Args...>>>
270 _S_construct(_Alloc&, _Tp* __p, _Args&&... __args)
271 noexcept(std::is_nothrow_constructible<_Tp, _Args...>::value)
272 {
273#if __cplusplus <= 201703L
274 ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
275#else
276 std::construct_at(__p, std::forward<_Args>(__args)...);
277#endif
278 }
279
280 template<typename _Alloc2, typename _Tp>
281 static _GLIBCXX14_CONSTEXPR auto
282 _S_destroy(_Alloc2& __a, _Tp* __p, int)
283 noexcept(noexcept(__a.destroy(__p)))
284 -> decltype(__a.destroy(__p))
285 { __a.destroy(__p); }
286
287 template<typename _Alloc2, typename _Tp>
288 static _GLIBCXX14_CONSTEXPR void
289 _S_destroy(_Alloc2&, _Tp* __p, ...)
290 noexcept(std::is_nothrow_destructible<_Tp>::value)
291 { std::_Destroy(__p); }
292
293 template<typename _Alloc2>
294 static constexpr auto
295 _S_max_size(_Alloc2& __a, int)
296 -> decltype(__a.max_size())
297 { return __a.max_size(); }
298
299 template<typename _Alloc2>
300 static constexpr size_type
301 _S_max_size(_Alloc2&, ...)
302 {
303 // _GLIBCXX_RESOLVE_LIB_DEFECTS
304 // 2466. allocator_traits::max_size() default behavior is incorrect
305 return __gnu_cxx::__numeric_traits<size_type>::__max
306 / sizeof(value_type);
307 }
308
309 template<typename _Alloc2>
310 static constexpr auto
311 _S_select(_Alloc2& __a, int)
312 -> decltype(__a.select_on_container_copy_construction())
313 { return __a.select_on_container_copy_construction(); }
314
315 template<typename _Alloc2>
316 static constexpr _Alloc2
317 _S_select(_Alloc2& __a, ...)
318 { return __a; }
319
320 public:
321
322 /**
323 * @brief Allocate memory.
324 * @param __a An allocator.
325 * @param __n The number of objects to allocate space for.
326 *
327 * Calls @c a.allocate(n)
328 */
329 _GLIBCXX_NODISCARD static _GLIBCXX20_CONSTEXPR pointer
330 allocate(_Alloc& __a, size_type __n)
331 { return __a.allocate(__n); }
332
333 /**
334 * @brief Allocate memory.
335 * @param __a An allocator.
336 * @param __n The number of objects to allocate space for.
337 * @param __hint Aid to locality.
338 * @return Memory of suitable size and alignment for @a n objects
339 * of type @c value_type
340 *
341 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
342 * well-formed, otherwise returns @c a.allocate(n)
343 */
344 _GLIBCXX_NODISCARD static _GLIBCXX20_CONSTEXPR pointer
345 allocate(_Alloc& __a, size_type __n, const_void_pointer __hint)
346 { return _S_allocate(__a, __n, __hint, 0); }
347
348 /**
349 * @brief Deallocate memory.
350 * @param __a An allocator.
351 * @param __p Pointer to the memory to deallocate.
352 * @param __n The number of objects space was allocated for.
353 *
354 * Calls <tt> a.deallocate(p, n) </tt>
355 */
356 static _GLIBCXX20_CONSTEXPR void
357 deallocate(_Alloc& __a, pointer __p, size_type __n)
358 { __a.deallocate(__p, __n); }
359
360 /**
361 * @brief Construct an object of type `_Tp`
362 * @param __a An allocator.
363 * @param __p Pointer to memory of suitable size and alignment for Tp
364 * @param __args Constructor arguments.
365 *
366 * Calls <tt> __a.construct(__p, std::forward<Args>(__args)...) </tt>
367 * if that expression is well-formed, otherwise uses placement-new
368 * to construct an object of type @a _Tp at location @a __p from the
369 * arguments @a __args...
370 */
371 template<typename _Tp, typename... _Args>
372 static _GLIBCXX20_CONSTEXPR auto
373 construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
374 noexcept(noexcept(_S_construct(__a, __p,
375 std::forward<_Args>(__args)...)))
376 -> decltype(_S_construct(__a, __p, std::forward<_Args>(__args)...))
377 { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
378
379 /**
380 * @brief Destroy an object of type @a _Tp
381 * @param __a An allocator.
382 * @param __p Pointer to the object to destroy
383 *
384 * Calls @c __a.destroy(__p) if that expression is well-formed,
385 * otherwise calls @c __p->~_Tp()
386 */
387 template<typename _Tp>
388 static _GLIBCXX20_CONSTEXPR void
389 destroy(_Alloc& __a, _Tp* __p)
390 noexcept(noexcept(_S_destroy(__a, __p, 0)))
391 { _S_destroy(__a, __p, 0); }
392
393 /**
394 * @brief The maximum supported allocation size
395 * @param __a An allocator.
396 * @return @c __a.max_size() or @c numeric_limits<size_type>::max()
397 *
398 * Returns @c __a.max_size() if that expression is well-formed,
399 * otherwise returns @c numeric_limits<size_type>::max()
400 */
401 static _GLIBCXX20_CONSTEXPR size_type
402 max_size(const _Alloc& __a) noexcept
403 { return _S_max_size(__a, 0); }
404
405 /**
406 * @brief Obtain an allocator to use when copying a container.
407 * @param __rhs An allocator.
408 * @return @c __rhs.select_on_container_copy_construction() or @a __rhs
409 *
410 * Returns @c __rhs.select_on_container_copy_construction() if that
411 * expression is well-formed, otherwise returns @a __rhs
412 */
413 static _GLIBCXX20_CONSTEXPR _Alloc
415 { return _S_select(__rhs, 0); }
416 };
417
418#if _GLIBCXX_HOSTED
419 /// Partial specialization for std::allocator.
420 template<typename _Tp>
422 {
423 /// The allocator type
425
426 /// The allocated type
427 using value_type = _Tp;
428
429 /// The allocator's pointer type.
430 using pointer = _Tp*;
431
432 /// The allocator's const pointer type.
433 using const_pointer = const _Tp*;
434
435 /// The allocator's void pointer type.
436 using void_pointer = void*;
437
438 /// The allocator's const void pointer type.
439 using const_void_pointer = const void*;
440
441 /// The allocator's difference type
442 using difference_type = std::ptrdiff_t;
443
444 /// The allocator's size type
445 using size_type = std::size_t;
446
447 /// How the allocator is propagated on copy assignment
449
450 /// How the allocator is propagated on move assignment
452
453 /// How the allocator is propagated on swap
455
456 /// Whether all instances of the allocator type compare equal.
458
459 template<typename _Up>
460 using rebind_alloc = allocator<_Up>;
461
462 template<typename _Up>
463 using rebind_traits = allocator_traits<allocator<_Up>>;
464
465 /**
466 * @brief Allocate memory.
467 * @param __a An allocator.
468 * @param __n The number of objects to allocate space for.
469 *
470 * Calls @c a.allocate(n)
471 */
472 [[__nodiscard__,__gnu__::__always_inline__]]
473 static _GLIBCXX20_CONSTEXPR pointer
475 { return __a.allocate(__n); }
476
477 /**
478 * @brief Allocate memory.
479 * @param __a An allocator.
480 * @param __n The number of objects to allocate space for.
481 * @param __hint Aid to locality.
482 * @return Memory of suitable size and alignment for @a n objects
483 * of type @c value_type
484 *
485 * Returns <tt> a.allocate(n, hint) </tt>
486 */
487 [[__nodiscard__,__gnu__::__always_inline__]]
488 static _GLIBCXX20_CONSTEXPR pointer
490 [[maybe_unused]] const_void_pointer __hint)
491 {
492#if __cplusplus <= 201703L
493 return __a.allocate(__n, __hint);
494#else
495 return __a.allocate(__n);
496#endif
497 }
498
499 /**
500 * @brief Deallocate memory.
501 * @param __a An allocator.
502 * @param __p Pointer to the memory to deallocate.
503 * @param __n The number of objects space was allocated for.
504 *
505 * Calls <tt> a.deallocate(p, n) </tt>
506 */
507 [[__gnu__::__always_inline__]]
508 static _GLIBCXX20_CONSTEXPR void
510 { __a.deallocate(__p, __n); }
511
512 /**
513 * @brief Construct an object of type `_Up`
514 * @param __a An allocator.
515 * @param __p Pointer to memory of suitable size and alignment for
516 * an object of type `_Up`.
517 * @param __args Constructor arguments.
518 *
519 * Calls `__a.construct(__p, std::forward<_Args>(__args)...)`
520 * in C++11, C++14 and C++17. Changed in C++20 to call
521 * `std::construct_at(__p, std::forward<_Args>(__args)...)` instead.
522 */
523 template<typename _Up, typename... _Args>
524 [[__gnu__::__always_inline__]]
525 static _GLIBCXX20_CONSTEXPR void
526 construct(allocator_type& __a __attribute__((__unused__)), _Up* __p,
527 _Args&&... __args)
528 noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
529 {
530#if __cplusplus <= 201703L
531 __a.construct(__p, std::forward<_Args>(__args)...);
532#else
533 std::construct_at(__p, std::forward<_Args>(__args)...);
534#endif
535 }
536
537 /**
538 * @brief Destroy an object of type @a _Up
539 * @param __a An allocator.
540 * @param __p Pointer to the object to destroy
541 *
542 * Calls @c __a.destroy(__p).
543 */
544 template<typename _Up>
545 [[__gnu__::__always_inline__]]
546 static _GLIBCXX20_CONSTEXPR void
547 destroy(allocator_type& __a __attribute__((__unused__)), _Up* __p)
549 {
550#if __cplusplus <= 201703L
551 __a.destroy(__p);
552#else
553 std::destroy_at(__p);
554#endif
555 }
556
557 /**
558 * @brief The maximum supported allocation size
559 * @param __a An allocator.
560 * @return @c __a.max_size()
561 */
562 [[__gnu__::__always_inline__]]
563 static _GLIBCXX20_CONSTEXPR size_type
564 max_size(const allocator_type& __a __attribute__((__unused__))) noexcept
565 {
566#if __cplusplus <= 201703L
567 return __a.max_size();
568#else
569 return size_t(-1) / sizeof(value_type);
570#endif
571 }
572
573 /**
574 * @brief Obtain an allocator to use when copying a container.
575 * @param __rhs An allocator.
576 * @return @c __rhs
577 */
578 [[__gnu__::__always_inline__]]
579 static _GLIBCXX20_CONSTEXPR allocator_type
581 { return __rhs; }
582 };
583
584 /// Explicit specialization for std::allocator<void>.
585 template<>
587 {
588 /// The allocator type
590
591 /// The allocated type
592 using value_type = void;
593
594 /// The allocator's pointer type.
595 using pointer = void*;
596
597 /// The allocator's const pointer type.
598 using const_pointer = const void*;
599
600 /// The allocator's void pointer type.
601 using void_pointer = void*;
602
603 /// The allocator's const void pointer type.
604 using const_void_pointer = const void*;
605
606 /// The allocator's difference type
607 using difference_type = std::ptrdiff_t;
608
609 /// The allocator's size type
610 using size_type = std::size_t;
611
612 /// How the allocator is propagated on copy assignment
614
615 /// How the allocator is propagated on move assignment
617
618 /// How the allocator is propagated on swap
620
621 /// Whether all instances of the allocator type compare equal.
623
624 template<typename _Up>
625 using rebind_alloc = allocator<_Up>;
626
627 template<typename _Up>
628 using rebind_traits = allocator_traits<allocator<_Up>>;
629
630 /// allocate is ill-formed for allocator<void>
631 static void*
632 allocate(allocator_type&, size_type, const void* = nullptr) = delete;
633
634 /// deallocate is ill-formed for allocator<void>
635 static void
637
638 /**
639 * @brief Construct an object of type `_Up`
640 * @param __a An allocator.
641 * @param __p Pointer to memory of suitable size and alignment for
642 * an object of type `_Up`.
643 * @param __args Constructor arguments.
644 *
645 * Calls `__a.construct(__p, std::forward<_Args>(__args)...)`
646 * in C++11, C++14 and C++17. Changed in C++20 to call
647 * `std::construct_at(__p, std::forward<_Args>(__args)...)` instead.
648 */
649 template<typename _Up, typename... _Args>
650 [[__gnu__::__always_inline__]]
651 static _GLIBCXX20_CONSTEXPR void
652 construct(allocator_type&, _Up* __p, _Args&&... __args)
653 noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
654 { std::_Construct(__p, std::forward<_Args>(__args)...); }
655
656 /**
657 * @brief Destroy an object of type `_Up`
658 * @param __a An allocator.
659 * @param __p Pointer to the object to destroy
660 *
661 * Invokes the destructor for `*__p`.
662 */
663 template<typename _Up>
664 [[__gnu__::__always_inline__]]
665 static _GLIBCXX20_CONSTEXPR void
669
670 /// max_size is ill-formed for allocator<void>
671 static size_type
672 max_size(const allocator_type&) = delete;
673
674 /**
675 * @brief Obtain an allocator to use when copying a container.
676 * @param __rhs An allocator.
677 * @return `__rhs`
678 */
679 [[__gnu__::__always_inline__]]
680 static _GLIBCXX20_CONSTEXPR allocator_type
682 { return __rhs; }
683 };
684#endif
685
686 /// @cond undocumented
687#if __cplusplus < 201703L
688 template<typename _Alloc>
689 [[__gnu__::__always_inline__]]
690 inline void
691 __do_alloc_on_copy(_Alloc& __one, const _Alloc& __two, true_type)
692 { __one = __two; }
693
694 template<typename _Alloc>
695 [[__gnu__::__always_inline__]]
696 inline void
697 __do_alloc_on_copy(_Alloc&, const _Alloc&, false_type)
698 { }
699#endif
700
701 template<typename _Alloc>
702 [[__gnu__::__always_inline__]]
703 _GLIBCXX14_CONSTEXPR inline void
704 __alloc_on_copy(_Alloc& __one, const _Alloc& __two)
705 {
706 using __traits = allocator_traits<_Alloc>;
707 using __pocca =
708 typename __traits::propagate_on_container_copy_assignment::type;
709#if __cplusplus >= 201703L
710 if constexpr (__pocca::value)
711 __one = __two;
712#else
713 __do_alloc_on_copy(__one, __two, __pocca());
714#endif
715 }
716
717 template<typename _Alloc>
718 [[__gnu__::__always_inline__]]
719 constexpr _Alloc
720 __alloc_on_copy(const _Alloc& __a)
721 {
722 typedef allocator_traits<_Alloc> __traits;
723 return __traits::select_on_container_copy_construction(__a);
724 }
725
726#if __cplusplus < 201703L
727 template<typename _Alloc>
728 [[__gnu__::__always_inline__]]
729 inline void __do_alloc_on_move(_Alloc& __one, _Alloc& __two, true_type)
730 { __one = std::move(__two); }
731
732 template<typename _Alloc>
733 [[__gnu__::__always_inline__]]
734 inline void __do_alloc_on_move(_Alloc&, _Alloc&, false_type)
735 { }
736#endif
737
738 template<typename _Alloc>
739 [[__gnu__::__always_inline__]]
740 _GLIBCXX14_CONSTEXPR inline void
741 __alloc_on_move(_Alloc& __one, _Alloc& __two)
742 {
743 using __traits = allocator_traits<_Alloc>;
744 using __pocma
745 = typename __traits::propagate_on_container_move_assignment::type;
746#if __cplusplus >= 201703L
747 if constexpr (__pocma::value)
748 __one = std::move(__two);
749#else
750 __do_alloc_on_move(__one, __two, __pocma());
751#endif
752 }
753
754#if __cplusplus < 201703L
755 template<typename _Alloc>
756 [[__gnu__::__always_inline__]]
757 inline void __do_alloc_on_swap(_Alloc& __one, _Alloc& __two, true_type)
758 {
759 using std::swap;
760 swap(__one, __two);
761 }
762
763 template<typename _Alloc>
764 [[__gnu__::__always_inline__]]
765 inline void __do_alloc_on_swap(_Alloc&, _Alloc&, false_type)
766 { }
767#endif
768
769 template<typename _Alloc>
770 [[__gnu__::__always_inline__]]
771 _GLIBCXX14_CONSTEXPR inline void
772 __alloc_on_swap(_Alloc& __one, _Alloc& __two)
773 {
774 using __traits = allocator_traits<_Alloc>;
775 using __pocs = typename __traits::propagate_on_container_swap::type;
776#if __cplusplus >= 201703L
777 if constexpr (__pocs::value)
778 {
779 using std::swap;
780 swap(__one, __two);
781 }
782#else
783 __do_alloc_on_swap(__one, __two, __pocs());
784#endif
785 }
786
787 template<typename _Alloc, typename _Tp,
788 typename _ValueT = __remove_cvref_t<typename _Alloc::value_type>,
789 typename = void>
790 struct __is_alloc_insertable_impl
791 : false_type
792 { };
793
794 template<typename _Alloc, typename _Tp, typename _ValueT>
795 struct __is_alloc_insertable_impl<_Alloc, _Tp, _ValueT,
796 __void_t<decltype(allocator_traits<_Alloc>::construct(
797 std::declval<_Alloc&>(), std::declval<_ValueT*>(),
798 std::declval<_Tp>()))>>
799 : true_type
800 { };
801
802 // true if _Alloc::value_type is CopyInsertable into containers using _Alloc
803 // (might be wrong if _Alloc::construct exists but is not constrained,
804 // i.e. actually trying to use it would still be invalid. Use with caution.)
805 template<typename _Alloc>
806 struct __is_copy_insertable
807 : __is_alloc_insertable_impl<_Alloc,
808 typename _Alloc::value_type const&>::type
809 { };
810
811#if _GLIBCXX_HOSTED
812 // std::allocator<_Tp> just requires CopyConstructible
813 template<typename _Tp>
814 struct __is_copy_insertable<allocator<_Tp>>
815 : is_copy_constructible<_Tp>
816 { };
817#endif
818
819 // true if _Alloc::value_type is MoveInsertable into containers using _Alloc
820 // (might be wrong if _Alloc::construct exists but is not constrained,
821 // i.e. actually trying to use it would still be invalid. Use with caution.)
822 template<typename _Alloc>
823 struct __is_move_insertable
824 : __is_alloc_insertable_impl<_Alloc, typename _Alloc::value_type>::type
825 { };
826
827#if _GLIBCXX_HOSTED
828 // std::allocator<_Tp> just requires MoveConstructible
829 template<typename _Tp>
830 struct __is_move_insertable<allocator<_Tp>>
831 : is_move_constructible<_Tp>
832 { };
833#endif
834
835 // Trait to detect Allocator-like types.
836 template<typename _Alloc, typename = void>
837 struct __is_allocator : false_type { };
838
839 template<typename _Alloc>
840 struct __is_allocator<_Alloc,
841 __void_t<typename _Alloc::value_type,
842 decltype(std::declval<_Alloc&>().allocate(size_t{}))>>
843 : true_type { };
844
845 template<typename _Alloc>
846 using _RequireAllocator
847 = typename enable_if<__is_allocator<_Alloc>::value, _Alloc>::type;
848
849 template<typename _Alloc>
850 using _RequireNotAllocator
851 = typename enable_if<!__is_allocator<_Alloc>::value, _Alloc>::type;
852
853#if __cpp_concepts >= 201907L
854 template<typename _Alloc>
855 concept __allocator_like = requires (_Alloc& __a) {
856 typename _Alloc::value_type;
857 __a.deallocate(__a.allocate(1u), 1u);
858 };
859#endif
860 /// @endcond
861#endif // C++11
862
863 /// @cond undocumented
864
865 // To implement Option 3 of DR 431.
866 template<typename _Alloc, bool = __is_empty(_Alloc)>
867 struct __alloc_swap
868 { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
869
870 template<typename _Alloc>
871 struct __alloc_swap<_Alloc, false>
872 {
873 static void
874 _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
875 {
876 // Precondition: swappable allocators.
877 if (__one != __two)
878 swap(__one, __two);
879 }
880 };
881
882#if __cplusplus >= 201103L
883 template<typename _Tp, bool
884 = __or_<is_copy_constructible<typename _Tp::value_type>,
885 is_nothrow_move_constructible<typename _Tp::value_type>>::value>
886 struct __shrink_to_fit_aux
887 { static bool _S_do_it(_Tp&) noexcept { return false; } };
888
889 template<typename _Tp>
890 struct __shrink_to_fit_aux<_Tp, true>
891 {
892 _GLIBCXX20_CONSTEXPR
893 static bool
894 _S_do_it(_Tp& __c) noexcept
895 {
896#if __cpp_exceptions
897 try
898 {
899 _Tp(__make_move_if_noexcept_iterator(__c.begin()),
900 __make_move_if_noexcept_iterator(__c.end()),
901 __c.get_allocator()).swap(__c);
902 return true;
903 }
904 catch(...)
905 { return false; }
906#else
907 return false;
908#endif
909 }
910 };
911#endif
912
913 /**
914 * Destroy a range of objects using the supplied allocator. For
915 * non-default allocators we do not optimize away invocation of
916 * destroy() even if _Tp has a trivial destructor.
917 */
918
919 template<typename _ForwardIterator, typename _Allocator>
920 _GLIBCXX20_CONSTEXPR
921 void
922 _Destroy(_ForwardIterator __first, _ForwardIterator __last,
923 _Allocator& __alloc)
924 {
925 for (; __first != __last; ++__first)
926#if __cplusplus < 201103L
927 __alloc.destroy(std::__addressof(*__first));
928#else
930 std::__addressof(*__first));
931#endif
932 }
933
934#if _GLIBCXX_HOSTED
935 template<typename _ForwardIterator, typename _Tp>
936 __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
937 inline void
938 _Destroy(_ForwardIterator __first, _ForwardIterator __last,
939 allocator<_Tp>&)
940 {
941 std::_Destroy(__first, __last);
942 }
943#endif
944 /// @endcond
945
946_GLIBCXX_END_NAMESPACE_VERSION
947} // namespace std
948#endif // _ALLOC_TRAITS_H
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:111
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:114
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:126
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
ISO C++ entities toplevel namespace is std.
constexpr void _Construct(_Tp *__p, _Args &&... __args)
constexpr void _Destroy(_ForwardIterator __first, _ForwardIterator __last)
is_nothrow_destructible
Definition type_traits:1102
is_nothrow_constructible
Definition type_traits:1185
Uniform interface to all allocator types.
static constexpr auto construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(noexcept(_S_construct(__a, __p, std::forward< _Args >(__args)...))) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp
__detected_or_t< false_type, __pocma, _Alloc > propagate_on_container_move_assignment
How the allocator is propagated on move assignment.
typename _Ptr< __v_pointer, void >::type void_pointer
The allocator's void pointer type.
__detected_or_t< value_type *, __pointer, _Alloc > pointer
The allocator's pointer type.
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
static constexpr pointer allocate(_Alloc &__a, size_type __n, const_void_pointer __hint)
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
The allocator's const void pointer type.
typename _Diff< _Alloc, pointer >::type difference_type
The allocator's difference type.
typename _Ptr< __c_pointer, const value_type >::type const_pointer
The allocator's const pointer type.
_Alloc::value_type value_type
The allocated type.
static constexpr void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
typename __detected_or_t< is_empty< _Alloc >, __equal, _Alloc >::type is_always_equal
Whether all instances of the allocator type compare equal.
static constexpr size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.
__detected_or_t< false_type, __pocca, _Alloc > propagate_on_container_copy_assignment
How the allocator is propagated on copy assignment.
static constexpr void destroy(_Alloc &__a, _Tp *__p) noexcept(noexcept(_S_destroy(__a, __p, 0)))
Destroy an object of type _Tp.
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, _Alloc > propagate_on_container_swap
How the allocator is propagated on swap.
_Alloc allocator_type
The allocator type.
allocator< _Tp > allocator_type
The allocator type.
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.
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 construct(allocator_type &__a, _Up *__p, _Args &&... __args) noexcept(std::is_nothrow_constructible< _Up, _Args... >::value)
Construct an object of type _Up
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>
static constexpr void construct(allocator_type &, _Up *__p, _Args &&... __args) noexcept(std::is_nothrow_constructible< _Up, _Args... >::value)
Construct an object of type _Up
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.
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:129
Uniform interface to all pointer-like types.
Definition ptr_traits.h:178