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