1 // Allocator traits -*- C++ -*-
3 // Copyright (C) 2011-2022 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
30 #ifndef _ALLOC_TRAITS_H
31 #define _ALLOC_TRAITS_H 1
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>
39 # include <bits/allocator.h>
43 namespace std
_GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 #if __cplusplus >= 201103L
48 #define __cpp_lib_allocator_traits_is_always_equal 201411L
50 /// @cond undocumented
51 struct __allocator_traits_base
53 template<typename _Tp
, typename _Up
, typename
= void>
54 struct __rebind
: __replace_first_arg
<_Tp
, _Up
> { };
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
; };
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
>;
80 template<typename _Alloc
, typename _Up
>
82 = typename
__allocator_traits_base::template __rebind
<_Alloc
, _Up
>::type
;
86 * @brief Uniform interface to all allocator types.
91 template<typename _Alloc
>
92 struct allocator_traits
: __allocator_traits_base
94 /// The allocator type
95 typedef _Alloc allocator_type
;
96 /// The allocated type
97 typedef typename
_Alloc::value_type value_type
;
100 * @brief The allocator's pointer type.
102 * @c Alloc::pointer if that type exists, otherwise @c value_type*
104 using pointer
= __detected_or_t
<value_type
*, __pointer
, _Alloc
>;
107 // Select _Func<_Alloc> or pointer_traits<pointer>::rebind<_Tp>
108 template<template<typename
> class _Func
, typename _Tp
, typename
= void>
111 using type
= typename pointer_traits
<pointer
>::template rebind
<_Tp
>;
114 template<template<typename
> class _Func
, typename _Tp
>
115 struct _Ptr
<_Func
, _Tp
, __void_t
<_Func
<_Alloc
>>>
117 using type
= _Func
<_Alloc
>;
120 // Select _A2::difference_type or pointer_traits<_Ptr>::difference_type
121 template<typename _A2
, typename _PtrT
, typename
= void>
123 { using type
= typename pointer_traits
<_PtrT
>::difference_type
; };
125 template<typename _A2
, typename _PtrT
>
126 struct _Diff
<_A2
, _PtrT
, __void_t
<typename
_A2::difference_type
>>
127 { using type
= typename
_A2::difference_type
; };
129 // Select _A2::size_type or make_unsigned<_DiffT>::type
130 template<typename _A2
, typename _DiffT
, typename
= void>
131 struct _Size
: make_unsigned
<_DiffT
> { };
133 template<typename _A2
, typename _DiffT
>
134 struct _Size
<_A2
, _DiffT
, __void_t
<typename
_A2::size_type
>>
135 { using type
= typename
_A2::size_type
; };
139 * @brief The allocator's const pointer type.
141 * @c Alloc::const_pointer if that type exists, otherwise
142 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
144 using const_pointer
= typename _Ptr
<__c_pointer
, const value_type
>::type
;
147 * @brief The allocator's void pointer type.
149 * @c Alloc::void_pointer if that type exists, otherwise
150 * <tt> pointer_traits<pointer>::rebind<void> </tt>
152 using void_pointer
= typename _Ptr
<__v_pointer
, void>::type
;
155 * @brief The allocator's const void pointer type.
157 * @c Alloc::const_void_pointer if that type exists, otherwise
158 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
160 using const_void_pointer
= typename _Ptr
<__cv_pointer
, const void>::type
;
163 * @brief The allocator's difference type
165 * @c Alloc::difference_type if that type exists, otherwise
166 * <tt> pointer_traits<pointer>::difference_type </tt>
168 using difference_type
= typename _Diff
<_Alloc
, pointer
>::type
;
171 * @brief The allocator's size type
173 * @c Alloc::size_type if that type exists, otherwise
174 * <tt> make_unsigned<difference_type>::type </tt>
176 using size_type
= typename _Size
<_Alloc
, difference_type
>::type
;
179 * @brief How the allocator is propagated on copy assignment
181 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
182 * otherwise @c false_type
184 using propagate_on_container_copy_assignment
185 = __detected_or_t
<false_type
, __pocca
, _Alloc
>;
188 * @brief How the allocator is propagated on move assignment
190 * @c Alloc::propagate_on_container_move_assignment if that type exists,
191 * otherwise @c false_type
193 using propagate_on_container_move_assignment
194 = __detected_or_t
<false_type
, __pocma
, _Alloc
>;
197 * @brief How the allocator is propagated on swap
199 * @c Alloc::propagate_on_container_swap if that type exists,
200 * otherwise @c false_type
202 using propagate_on_container_swap
203 = __detected_or_t
<false_type
, __pocs
, _Alloc
>;
206 * @brief Whether all instances of the allocator type compare equal.
208 * @c Alloc::is_always_equal if that type exists,
209 * otherwise @c is_empty<Alloc>::type
211 using is_always_equal
212 = typename __detected_or_t
<is_empty
<_Alloc
>, __equal
, _Alloc
>::type
;
214 template<typename _Tp
>
215 using rebind_alloc
= __alloc_rebind
<_Alloc
, _Tp
>;
216 template<typename _Tp
>
217 using rebind_traits
= allocator_traits
<rebind_alloc
<_Tp
>>;
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
); }
226 template<typename _Alloc2
>
227 static constexpr pointer
228 _S_allocate(_Alloc2
& __a
, size_type __n
, const_void_pointer
, ...)
229 { return __a
.allocate(__n
); }
231 template<typename _Tp
, typename
... _Args
>
232 struct __construct_helper
234 template<typename _Alloc2
,
235 typename
= decltype(std::declval
<_Alloc2
*>()->construct(
236 std::declval
<_Tp
*>(), std::declval
<_Args
>()...))>
237 static true_type
__test(int);
240 static false_type
__test(...);
242 using type
= decltype(__test
<_Alloc
>(0));
245 template<typename _Tp
, typename
... _Args
>
246 using __has_construct
247 = typename __construct_helper
<_Tp
, _Args
...>::type
;
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
)...); }
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
)
262 #if __cplusplus <= 201703L
263 ::new((void*)__p
) _Tp(std::forward
<_Args
>(__args
)...);
265 std::construct_at(__p
, std::forward
<_Args
>(__args
)...);
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
); }
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
); }
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(); }
288 template<typename _Alloc2
>
289 static constexpr size_type
290 _S_max_size(_Alloc2
&, ...)
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
);
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(); }
304 template<typename _Alloc2
>
305 static constexpr _Alloc2
306 _S_select(_Alloc2
& __a
, ...)
312 * @brief Allocate memory.
313 * @param __a An allocator.
314 * @param __n The number of objects to allocate space for.
316 * Calls @c a.allocate(n)
318 _GLIBCXX_NODISCARD
static _GLIBCXX20_CONSTEXPR pointer
319 allocate(_Alloc
& __a
, size_type __n
)
320 { return __a
.allocate(__n
); }
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
330 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
331 * well-formed, otherwise returns @c a.allocate(n)
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); }
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.
343 * Calls <tt> a.deallocate(p, n) </tt>
345 static _GLIBCXX20_CONSTEXPR
void
346 deallocate(_Alloc
& __a
, pointer __p
, size_type __n
)
347 { __a
.deallocate(__p
, __n
); }
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.
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...
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
)...); }
369 * @brief Destroy an object of type @a _Tp
370 * @param __a An allocator.
371 * @param __p Pointer to the object to destroy
373 * Calls @c __a.destroy(__p) if that expression is well-formed,
374 * otherwise calls @c __p->~_Tp()
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); }
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()
387 * Returns @c __a.max_size() if that expression is well-formed,
388 * otherwise returns @c numeric_limits<size_type>::max()
390 static _GLIBCXX20_CONSTEXPR size_type
391 max_size(const _Alloc
& __a
) noexcept
392 { return _S_max_size(__a
, 0); }
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
399 * Returns @c __rhs.select_on_container_copy_construction() if that
400 * expression is well-formed, otherwise returns @a __rhs
402 static _GLIBCXX20_CONSTEXPR _Alloc
403 select_on_container_copy_construction(const _Alloc
& __rhs
)
404 { return _S_select(__rhs
, 0); }
409 #if __cplusplus > 201703L
410 # define __cpp_lib_constexpr_dynamic_alloc 201907L
413 /// Partial specialization for std::allocator.
414 template<typename _Tp
>
415 struct allocator_traits
<allocator
<_Tp
>>
417 /// The allocator type
418 using allocator_type
= allocator
<_Tp
>;
420 /// The allocated type
421 using value_type
= _Tp
;
423 /// The allocator's pointer type.
424 using pointer
= _Tp
*;
426 /// The allocator's const pointer type.
427 using const_pointer
= const _Tp
*;
429 /// The allocator's void pointer type.
430 using void_pointer
= void*;
432 /// The allocator's const void pointer type.
433 using const_void_pointer
= const void*;
435 /// The allocator's difference type
436 using difference_type
= std::ptrdiff_t;
438 /// The allocator's size type
439 using size_type
= std::size_t;
441 /// How the allocator is propagated on copy assignment
442 using propagate_on_container_copy_assignment
= false_type
;
444 /// How the allocator is propagated on move assignment
445 using propagate_on_container_move_assignment
= true_type
;
447 /// How the allocator is propagated on swap
448 using propagate_on_container_swap
= false_type
;
450 /// Whether all instances of the allocator type compare equal.
451 using is_always_equal
= true_type
;
453 template<typename _Up
>
454 using rebind_alloc
= allocator
<_Up
>;
456 template<typename _Up
>
457 using rebind_traits
= allocator_traits
<allocator
<_Up
>>;
460 * @brief Allocate memory.
461 * @param __a An allocator.
462 * @param __n The number of objects to allocate space for.
464 * Calls @c a.allocate(n)
466 [[__nodiscard__
,__gnu__::__always_inline__
]]
467 static _GLIBCXX20_CONSTEXPR pointer
468 allocate(allocator_type
& __a
, size_type __n
)
469 { return __a
.allocate(__n
); }
472 * @brief Allocate memory.
473 * @param __a An allocator.
474 * @param __n The number of objects to allocate space for.
475 * @param __hint Aid to locality.
476 * @return Memory of suitable size and alignment for @a n objects
477 * of type @c value_type
479 * Returns <tt> a.allocate(n, hint) </tt>
481 [[__nodiscard__
,__gnu__::__always_inline__
]]
482 static _GLIBCXX20_CONSTEXPR pointer
483 allocate(allocator_type
& __a
, size_type __n
, const_void_pointer __hint
)
485 #if __cplusplus <= 201703L
486 return __a
.allocate(__n
, __hint
);
488 return __a
.allocate(__n
);
493 * @brief Deallocate memory.
494 * @param __a An allocator.
495 * @param __p Pointer to the memory to deallocate.
496 * @param __n The number of objects space was allocated for.
498 * Calls <tt> a.deallocate(p, n) </tt>
500 [[__gnu__::__always_inline__
]]
501 static _GLIBCXX20_CONSTEXPR
void
502 deallocate(allocator_type
& __a
, pointer __p
, size_type __n
)
503 { __a
.deallocate(__p
, __n
); }
506 * @brief Construct an object of type `_Up`
507 * @param __a An allocator.
508 * @param __p Pointer to memory of suitable size and alignment for
509 * an object of type `_Up`.
510 * @param __args Constructor arguments.
512 * Calls `__a.construct(__p, std::forward<_Args>(__args)...)`
513 * in C++11, C++14 and C++17. Changed in C++20 to call
514 * `std::construct_at(__p, std::forward<_Args>(__args)...)` instead.
516 template<typename _Up
, typename
... _Args
>
517 [[__gnu__::__always_inline__
]]
518 static _GLIBCXX20_CONSTEXPR
void
519 construct(allocator_type
& __a
__attribute__((__unused__
)), _Up
* __p
,
521 noexcept(std::is_nothrow_constructible
<_Up
, _Args
...>::value
)
523 #if __cplusplus <= 201703L
524 __a
.construct(__p
, std::forward
<_Args
>(__args
)...);
526 std::construct_at(__p
, std::forward
<_Args
>(__args
)...);
531 * @brief Destroy an object of type @a _Up
532 * @param __a An allocator.
533 * @param __p Pointer to the object to destroy
535 * Calls @c __a.destroy(__p).
537 template<typename _Up
>
538 [[__gnu__::__always_inline__
]]
539 static _GLIBCXX20_CONSTEXPR
void
540 destroy(allocator_type
& __a
__attribute__((__unused__
)), _Up
* __p
)
541 noexcept(is_nothrow_destructible
<_Up
>::value
)
543 #if __cplusplus <= 201703L
546 std::destroy_at(__p
);
551 * @brief The maximum supported allocation size
552 * @param __a An allocator.
553 * @return @c __a.max_size()
555 [[__gnu__::__always_inline__
]]
556 static _GLIBCXX20_CONSTEXPR size_type
557 max_size(const allocator_type
& __a
__attribute__((__unused__
))) noexcept
559 #if __cplusplus <= 201703L
560 return __a
.max_size();
562 return size_t(-1) / sizeof(value_type
);
567 * @brief Obtain an allocator to use when copying a container.
568 * @param __rhs An allocator.
571 [[__gnu__::__always_inline__
]]
572 static _GLIBCXX20_CONSTEXPR allocator_type
573 select_on_container_copy_construction(const allocator_type
& __rhs
)
577 /// Explicit specialization for std::allocator<void>.
579 struct allocator_traits
<allocator
<void>>
581 /// The allocator type
582 using allocator_type
= allocator
<void>;
584 /// The allocated type
585 using value_type
= void;
587 /// The allocator's pointer type.
588 using pointer
= void*;
590 /// The allocator's const pointer type.
591 using const_pointer
= const void*;
593 /// The allocator's void pointer type.
594 using void_pointer
= void*;
596 /// The allocator's const void pointer type.
597 using const_void_pointer
= const void*;
599 /// The allocator's difference type
600 using difference_type
= std::ptrdiff_t;
602 /// The allocator's size type
603 using size_type
= std::size_t;
605 /// How the allocator is propagated on copy assignment
606 using propagate_on_container_copy_assignment
= false_type
;
608 /// How the allocator is propagated on move assignment
609 using propagate_on_container_move_assignment
= true_type
;
611 /// How the allocator is propagated on swap
612 using propagate_on_container_swap
= false_type
;
614 /// Whether all instances of the allocator type compare equal.
615 using is_always_equal
= true_type
;
617 template<typename _Up
>
618 using rebind_alloc
= allocator
<_Up
>;
620 template<typename _Up
>
621 using rebind_traits
= allocator_traits
<allocator
<_Up
>>;
623 /// allocate is ill-formed for allocator<void>
625 allocate(allocator_type
&, size_type
, const void* = nullptr) = delete;
627 /// deallocate is ill-formed for allocator<void>
629 deallocate(allocator_type
&, void*, size_type
) = delete;
632 * @brief Construct an object of type `_Up`
633 * @param __a An allocator.
634 * @param __p Pointer to memory of suitable size and alignment for
635 * an object of type `_Up`.
636 * @param __args Constructor arguments.
638 * Calls `__a.construct(__p, std::forward<_Args>(__args)...)`
639 * in C++11, C++14 and C++17. Changed in C++20 to call
640 * `std::construct_at(__p, std::forward<_Args>(__args)...)` instead.
642 template<typename _Up
, typename
... _Args
>
643 [[__gnu__::__always_inline__
]]
644 static _GLIBCXX20_CONSTEXPR
void
645 construct(allocator_type
&, _Up
* __p
, _Args
&&... __args
)
646 noexcept(std::is_nothrow_constructible
<_Up
, _Args
...>::value
)
647 { std::_Construct(__p
, std::forward
<_Args
>(__args
)...); }
650 * @brief Destroy an object of type `_Up`
651 * @param __a An allocator.
652 * @param __p Pointer to the object to destroy
654 * Invokes the destructor for `*__p`.
656 template<typename _Up
>
657 [[__gnu__::__always_inline__
]]
658 static _GLIBCXX20_CONSTEXPR
void
659 destroy(allocator_type
&, _Up
* __p
)
660 noexcept(is_nothrow_destructible
<_Up
>::value
)
661 { std::_Destroy(__p
); }
663 /// max_size is ill-formed for allocator<void>
665 max_size(const allocator_type
&) = delete;
668 * @brief Obtain an allocator to use when copying a container.
669 * @param __rhs An allocator.
672 [[__gnu__::__always_inline__
]]
673 static _GLIBCXX20_CONSTEXPR allocator_type
674 select_on_container_copy_construction(const allocator_type
& __rhs
)
679 /// @cond undocumented
680 #if __cplusplus < 201703L
681 template<typename _Alloc
>
682 [[__gnu__::__always_inline__
]]
684 __do_alloc_on_copy(_Alloc
& __one
, const _Alloc
& __two
, true_type
)
687 template<typename _Alloc
>
688 [[__gnu__::__always_inline__
]]
690 __do_alloc_on_copy(_Alloc
&, const _Alloc
&, false_type
)
694 template<typename _Alloc
>
695 [[__gnu__::__always_inline__
]]
696 _GLIBCXX14_CONSTEXPR
inline void
697 __alloc_on_copy(_Alloc
& __one
, const _Alloc
& __two
)
699 using __traits
= allocator_traits
<_Alloc
>;
701 typename
__traits::propagate_on_container_copy_assignment::type
;
702 #if __cplusplus >= 201703L
703 if constexpr (__pocca::value
)
706 __do_alloc_on_copy(__one
, __two
, __pocca());
710 template<typename _Alloc
>
711 [[__gnu__::__always_inline__
]]
713 __alloc_on_copy(const _Alloc
& __a
)
715 typedef allocator_traits
<_Alloc
> __traits
;
716 return __traits::select_on_container_copy_construction(__a
);
719 #if __cplusplus < 201703L
720 template<typename _Alloc
>
721 [[__gnu__::__always_inline__
]]
722 inline void __do_alloc_on_move(_Alloc
& __one
, _Alloc
& __two
, true_type
)
723 { __one
= std::move(__two
); }
725 template<typename _Alloc
>
726 [[__gnu__::__always_inline__
]]
727 inline void __do_alloc_on_move(_Alloc
&, _Alloc
&, false_type
)
731 template<typename _Alloc
>
732 [[__gnu__::__always_inline__
]]
733 _GLIBCXX14_CONSTEXPR
inline void
734 __alloc_on_move(_Alloc
& __one
, _Alloc
& __two
)
736 using __traits
= allocator_traits
<_Alloc
>;
738 = typename
__traits::propagate_on_container_move_assignment::type
;
739 #if __cplusplus >= 201703L
740 if constexpr (__pocma::value
)
741 __one
= std::move(__two
);
743 __do_alloc_on_move(__one
, __two
, __pocma());
747 #if __cplusplus < 201703L
748 template<typename _Alloc
>
749 [[__gnu__::__always_inline__
]]
750 inline void __do_alloc_on_swap(_Alloc
& __one
, _Alloc
& __two
, true_type
)
756 template<typename _Alloc
>
757 [[__gnu__::__always_inline__
]]
758 inline void __do_alloc_on_swap(_Alloc
&, _Alloc
&, false_type
)
762 template<typename _Alloc
>
763 [[__gnu__::__always_inline__
]]
764 _GLIBCXX14_CONSTEXPR
inline void
765 __alloc_on_swap(_Alloc
& __one
, _Alloc
& __two
)
767 using __traits
= allocator_traits
<_Alloc
>;
768 using __pocs
= typename
__traits::propagate_on_container_swap::type
;
769 #if __cplusplus >= 201703L
770 if constexpr (__pocs::value
)
776 __do_alloc_on_swap(__one
, __two
, __pocs());
780 template<typename _Alloc
, typename _Tp
,
781 typename _ValueT
= __remove_cvref_t
<typename
_Alloc::value_type
>,
783 struct __is_alloc_insertable_impl
787 template<typename _Alloc
, typename _Tp
, typename _ValueT
>
788 struct __is_alloc_insertable_impl
<_Alloc
, _Tp
, _ValueT
,
789 __void_t
<decltype(allocator_traits
<_Alloc
>::construct(
790 std::declval
<_Alloc
&>(), std::declval
<_ValueT
*>(),
791 std::declval
<_Tp
>()))>>
795 // true if _Alloc::value_type is CopyInsertable into containers using _Alloc
796 // (might be wrong if _Alloc::construct exists but is not constrained,
797 // i.e. actually trying to use it would still be invalid. Use with caution.)
798 template<typename _Alloc
>
799 struct __is_copy_insertable
800 : __is_alloc_insertable_impl
<_Alloc
,
801 typename
_Alloc::value_type
const&>::type
805 // std::allocator<_Tp> just requires CopyConstructible
806 template<typename _Tp
>
807 struct __is_copy_insertable
<allocator
<_Tp
>>
808 : is_copy_constructible
<_Tp
>
812 // true if _Alloc::value_type is MoveInsertable into containers using _Alloc
813 // (might be wrong if _Alloc::construct exists but is not constrained,
814 // i.e. actually trying to use it would still be invalid. Use with caution.)
815 template<typename _Alloc
>
816 struct __is_move_insertable
817 : __is_alloc_insertable_impl
<_Alloc
, typename
_Alloc::value_type
>::type
821 // std::allocator<_Tp> just requires MoveConstructible
822 template<typename _Tp
>
823 struct __is_move_insertable
<allocator
<_Tp
>>
824 : is_move_constructible
<_Tp
>
828 // Trait to detect Allocator-like types.
829 template<typename _Alloc
, typename
= void>
830 struct __is_allocator
: false_type
{ };
832 template<typename _Alloc
>
833 struct __is_allocator
<_Alloc
,
834 __void_t
<typename
_Alloc::value_type
,
835 decltype(std::declval
<_Alloc
&>().allocate(size_t{}))>>
838 template<typename _Alloc
>
839 using _RequireAllocator
840 = typename enable_if
<__is_allocator
<_Alloc
>::value
, _Alloc
>::type
;
842 template<typename _Alloc
>
843 using _RequireNotAllocator
844 = typename enable_if
<!__is_allocator
<_Alloc
>::value
, _Alloc
>::type
;
846 #if __cpp_concepts >= 201907L
847 template<typename _Alloc
>
848 concept __allocator_like
= requires (_Alloc
& __a
) {
849 typename
_Alloc::value_type
;
850 __a
.deallocate(__a
.allocate(1u), 1u);
856 /// @cond undocumented
858 // To implement Option 3 of DR 431.
859 template<typename _Alloc
, bool = __is_empty(_Alloc
)>
861 { static void _S_do_it(_Alloc
&, _Alloc
&) _GLIBCXX_NOEXCEPT
{ } };
863 template<typename _Alloc
>
864 struct __alloc_swap
<_Alloc
, false>
867 _S_do_it(_Alloc
& __one
, _Alloc
& __two
) _GLIBCXX_NOEXCEPT
869 // Precondition: swappable allocators.
875 #if __cplusplus >= 201103L
876 template<typename _Tp
, bool
877 = __or_
<is_copy_constructible
<typename
_Tp::value_type
>,
878 is_nothrow_move_constructible
<typename
_Tp::value_type
>>::value
>
879 struct __shrink_to_fit_aux
880 { static bool _S_do_it(_Tp
&) noexcept
{ return false; } };
882 template<typename _Tp
>
883 struct __shrink_to_fit_aux
<_Tp
, true>
887 _S_do_it(_Tp
& __c
) noexcept
892 _Tp(__make_move_if_noexcept_iterator(__c
.begin()),
893 __make_move_if_noexcept_iterator(__c
.end()),
894 __c
.get_allocator()).swap(__c
);
907 * Destroy a range of objects using the supplied allocator. For
908 * non-default allocators we do not optimize away invocation of
909 * destroy() even if _Tp has a trivial destructor.
912 template<typename _ForwardIterator
, typename _Allocator
>
915 _Destroy(_ForwardIterator __first
, _ForwardIterator __last
,
918 for (; __first
!= __last
; ++__first
)
919 #if __cplusplus < 201103L
920 __alloc
.destroy(std::__addressof(*__first
));
922 allocator_traits
<_Allocator
>::destroy(__alloc
,
923 std::__addressof(*__first
));
928 template<typename _ForwardIterator
, typename _Tp
>
929 __attribute__((__always_inline__
)) _GLIBCXX20_CONSTEXPR
931 _Destroy(_ForwardIterator __first
, _ForwardIterator __last
,
934 _Destroy(__first
, __last
);
939 _GLIBCXX_END_NAMESPACE_VERSION
941 #endif // _ALLOC_TRAITS_H