1 // Allocator traits -*- C++ -*-
3 // Copyright (C) 2011-2015 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 #if __cplusplus >= 201103L
35 #include <bits/memoryfwd.h>
36 #include <bits/ptr_traits.h>
37 #include <ext/numeric_traits.h>
39 #define __cpp_lib_allocator_traits_is_always_equal 201411
41 namespace std
_GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 struct __allocator_traits_base
47 template<typename _Alloc
, typename _Up
>
48 using __rebind
= typename
_Alloc::template rebind
<_Up
>::other
;
51 template<typename _Tp
>
52 using __pointer
= typename
_Tp::pointer
;
53 template<typename _Tp
>
54 using __c_pointer
= typename
_Tp::const_pointer
;
55 template<typename _Tp
>
56 using __v_pointer
= typename
_Tp::void_pointer
;
57 template<typename _Tp
>
58 using __cv_pointer
= typename
_Tp::const_void_pointer
;
59 template<typename _Tp
>
60 using __diff_type
= typename
_Tp::difference_type
;
61 template<typename _Tp
>
62 using __size_type
= typename
_Tp::size_type
;
63 template<typename _Tp
>
64 using __pocca
= typename
_Tp::propagate_on_container_copy_assignment
;
65 template<typename _Tp
>
66 using __pocma
= typename
_Tp::propagate_on_container_move_assignment
;
67 template<typename _Tp
>
68 using __pocs
= typename
_Tp::propagate_on_container_swap
;
69 template<typename _Tp
>
70 using __equal
= typename
_Tp::is_always_equal
;
73 template<typename _Alloc
, typename _Up
>
74 using __alloc_rebind
= __detected_or_t_
<__replace_first_arg_t
,
75 __allocator_traits_base::__rebind
,
79 * @brief Uniform interface to all allocator types.
82 template<typename _Alloc
>
83 struct allocator_traits
: __allocator_traits_base
85 /// The allocator type
86 typedef _Alloc allocator_type
;
87 /// The allocated type
88 typedef typename
_Alloc::value_type value_type
;
91 * @brief The allocator's pointer type.
93 * @c Alloc::pointer if that type exists, otherwise @c value_type*
95 using pointer
= __detected_or_t
<value_type
*, __pointer
, _Alloc
>;
98 * @brief The allocator's const pointer type.
100 * @c Alloc::const_pointer if that type exists, otherwise
101 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
104 = __detected_or_t
<__ptr_rebind
<pointer
, const value_type
>,
105 __c_pointer
, _Alloc
>;
108 * @brief The allocator's void pointer type.
110 * @c Alloc::void_pointer if that type exists, otherwise
111 * <tt> pointer_traits<pointer>::rebind<void> </tt>
114 = __detected_or_t
<__ptr_rebind
<pointer
, void>, __v_pointer
, _Alloc
>;
117 * @brief The allocator's const void pointer type.
119 * @c Alloc::const_void_pointer if that type exists, otherwise
120 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
122 using const_void_pointer
123 = __detected_or_t
<__ptr_rebind
<pointer
, const void>, __cv_pointer
,
127 * @brief The allocator's difference type
129 * @c Alloc::difference_type if that type exists, otherwise
130 * <tt> pointer_traits<pointer>::difference_type </tt>
132 using difference_type
133 = __detected_or_t
<typename pointer_traits
<pointer
>::difference_type
,
134 __diff_type
, _Alloc
>;
137 * @brief The allocator's size type
139 * @c Alloc::size_type if that type exists, otherwise
140 * <tt> make_unsigned<difference_type>::type </tt>
143 = __detected_or_t
<typename make_unsigned
<difference_type
>::type
,
144 __size_type
, _Alloc
>;
147 * @brief How the allocator is propagated on copy assignment
149 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
150 * otherwise @c false_type
152 using propagate_on_container_copy_assignment
153 = __detected_or_t
<false_type
, __pocca
, _Alloc
>;
156 * @brief How the allocator is propagated on move assignment
158 * @c Alloc::propagate_on_container_move_assignment if that type exists,
159 * otherwise @c false_type
161 using propagate_on_container_move_assignment
162 = __detected_or_t
<false_type
, __pocma
, _Alloc
>;
165 * @brief How the allocator is propagated on swap
167 * @c Alloc::propagate_on_container_swap if that type exists,
168 * otherwise @c false_type
170 using propagate_on_container_swap
171 = __detected_or_t
<false_type
, __pocs
, _Alloc
>;
174 * @brief Whether all instances of the allocator type compare equal.
176 * @c Alloc::is_always_equal if that type exists,
177 * otherwise @c is_empty<Alloc>::type
179 using is_always_equal
180 = __detected_or_t
<typename is_empty
<_Alloc
>::type
, __equal
, _Alloc
>;
182 template<typename _Tp
>
183 using rebind_alloc
= __alloc_rebind
<_Alloc
, _Tp
>;
184 template<typename _Tp
>
185 using rebind_traits
= allocator_traits
<rebind_alloc
<_Tp
>>;
187 static_assert(!is_same
<rebind_alloc
<value_type
>, __undefined
>::value
,
188 "allocator defines rebind or is like Alloc<T, Args>");
191 template<typename _Alloc2
>
193 _S_allocate(_Alloc2
& __a
, size_type __n
, const_void_pointer __hint
, int)
194 -> decltype(__a
.allocate(__n
, __hint
))
195 { return __a
.allocate(__n
, __hint
); }
197 template<typename _Alloc2
>
199 _S_allocate(_Alloc2
& __a
, size_type __n
, const_void_pointer
, ...)
200 { return __a
.allocate(__n
); }
202 template<typename _Tp
, typename
... _Args
>
203 struct __construct_helper
205 template<typename _Alloc2
,
206 typename
= decltype(std::declval
<_Alloc2
*>()->construct(
207 std::declval
<_Tp
*>(), std::declval
<_Args
>()...))>
208 static true_type
__test(int);
211 static false_type
__test(...);
213 using type
= decltype(__test
<_Alloc
>(0));
216 template<typename _Tp
, typename
... _Args
>
217 using __has_construct
218 = typename __construct_helper
<_Tp
, _Args
...>::type
;
220 template<typename _Tp
, typename
... _Args
>
221 static _Require
<__has_construct
<_Tp
, _Args
...>>
222 _S_construct(_Alloc
& __a
, _Tp
* __p
, _Args
&&... __args
)
223 { __a
.construct(__p
, std::forward
<_Args
>(__args
)...); }
225 template<typename _Tp
, typename
... _Args
>
227 _Require
<__and_
<__not_
<__has_construct
<_Tp
, _Args
...>>,
228 is_constructible
<_Tp
, _Args
...>>>
229 _S_construct(_Alloc
&, _Tp
* __p
, _Args
&&... __args
)
230 { ::new((void*)__p
) _Tp(std::forward
<_Args
>(__args
)...); }
232 template<typename _Alloc2
, typename _Tp
>
234 _S_destroy(_Alloc2
& __a
, _Tp
* __p
, int)
235 -> decltype(__a
.destroy(__p
))
236 { __a
.destroy(__p
); }
238 template<typename _Alloc2
, typename _Tp
>
240 _S_destroy(_Alloc2
&, _Tp
* __p
, ...)
243 template<typename _Alloc2
>
245 _S_max_size(_Alloc2
& __a
, int)
246 -> decltype(__a
.max_size())
247 { return __a
.max_size(); }
249 template<typename _Alloc2
>
251 _S_max_size(_Alloc2
&, ...)
253 // _GLIBCXX_RESOLVE_LIB_DEFECTS
254 // 2466. allocator_traits::max_size() default behavior is incorrect
255 return __gnu_cxx::__numeric_traits
<size_type
>::__max
256 / sizeof(value_type
);
259 template<typename _Alloc2
>
261 _S_select(_Alloc2
& __a
, int)
262 -> decltype(__a
.select_on_container_copy_construction())
263 { return __a
.select_on_container_copy_construction(); }
265 template<typename _Alloc2
>
267 _S_select(_Alloc2
& __a
, ...)
273 * @brief Allocate memory.
274 * @param __a An allocator.
275 * @param __n The number of objects to allocate space for.
277 * Calls @c a.allocate(n)
280 allocate(_Alloc
& __a
, size_type __n
)
281 { return __a
.allocate(__n
); }
284 * @brief Allocate memory.
285 * @param __a An allocator.
286 * @param __n The number of objects to allocate space for.
287 * @param __hint Aid to locality.
288 * @return Memory of suitable size and alignment for @a n objects
289 * of type @c value_type
291 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
292 * well-formed, otherwise returns @c a.allocate(n)
295 allocate(_Alloc
& __a
, size_type __n
, const_void_pointer __hint
)
296 { return _S_allocate(__a
, __n
, __hint
, 0); }
299 * @brief Deallocate memory.
300 * @param __a An allocator.
301 * @param __p Pointer to the memory to deallocate.
302 * @param __n The number of objects space was allocated for.
304 * Calls <tt> a.deallocate(p, n) </tt>
307 deallocate(_Alloc
& __a
, pointer __p
, size_type __n
)
308 { __a
.deallocate(__p
, __n
); }
311 * @brief Construct an object of type @a _Tp
312 * @param __a An allocator.
313 * @param __p Pointer to memory of suitable size and alignment for Tp
314 * @param __args Constructor arguments.
316 * Calls <tt> __a.construct(__p, std::forward<Args>(__args)...) </tt>
317 * if that expression is well-formed, otherwise uses placement-new
318 * to construct an object of type @a _Tp at location @a __p from the
319 * arguments @a __args...
321 template<typename _Tp
, typename
... _Args
>
322 static auto construct(_Alloc
& __a
, _Tp
* __p
, _Args
&&... __args
)
323 -> decltype(_S_construct(__a
, __p
, std::forward
<_Args
>(__args
)...))
324 { _S_construct(__a
, __p
, std::forward
<_Args
>(__args
)...); }
327 * @brief Destroy an object of type @a _Tp
328 * @param __a An allocator.
329 * @param __p Pointer to the object to destroy
331 * Calls @c __a.destroy(__p) if that expression is well-formed,
332 * otherwise calls @c __p->~_Tp()
335 static void destroy(_Alloc
& __a
, _Tp
* __p
)
336 { _S_destroy(__a
, __p
, 0); }
339 * @brief The maximum supported allocation size
340 * @param __a An allocator.
341 * @return @c __a.max_size() or @c numeric_limits<size_type>::max()
343 * Returns @c __a.max_size() if that expression is well-formed,
344 * otherwise returns @c numeric_limits<size_type>::max()
346 static size_type
max_size(const _Alloc
& __a
) noexcept
347 { return _S_max_size(__a
, 0); }
350 * @brief Obtain an allocator to use when copying a container.
351 * @param __rhs An allocator.
352 * @return @c __rhs.select_on_container_copy_construction() or @a __rhs
354 * Returns @c __rhs.select_on_container_copy_construction() if that
355 * expression is well-formed, otherwise returns @a __rhs
358 select_on_container_copy_construction(const _Alloc
& __rhs
)
359 { return _S_select(__rhs
, 0); }
362 template<typename _Alloc
>
364 __do_alloc_on_copy(_Alloc
& __one
, const _Alloc
& __two
, true_type
)
367 template<typename _Alloc
>
369 __do_alloc_on_copy(_Alloc
&, const _Alloc
&, false_type
)
372 template<typename _Alloc
>
373 inline void __alloc_on_copy(_Alloc
& __one
, const _Alloc
& __two
)
375 typedef allocator_traits
<_Alloc
> __traits
;
376 typedef typename
__traits::propagate_on_container_copy_assignment __pocca
;
377 __do_alloc_on_copy(__one
, __two
, __pocca());
380 template<typename _Alloc
>
381 inline _Alloc
__alloc_on_copy(const _Alloc
& __a
)
383 typedef allocator_traits
<_Alloc
> __traits
;
384 return __traits::select_on_container_copy_construction(__a
);
387 template<typename _Alloc
>
388 inline void __do_alloc_on_move(_Alloc
& __one
, _Alloc
& __two
, true_type
)
389 { __one
= std::move(__two
); }
391 template<typename _Alloc
>
392 inline void __do_alloc_on_move(_Alloc
&, _Alloc
&, false_type
)
395 template<typename _Alloc
>
396 inline void __alloc_on_move(_Alloc
& __one
, _Alloc
& __two
)
398 typedef allocator_traits
<_Alloc
> __traits
;
399 typedef typename
__traits::propagate_on_container_move_assignment __pocma
;
400 __do_alloc_on_move(__one
, __two
, __pocma());
403 template<typename _Alloc
>
404 inline void __do_alloc_on_swap(_Alloc
& __one
, _Alloc
& __two
, true_type
)
410 template<typename _Alloc
>
411 inline void __do_alloc_on_swap(_Alloc
&, _Alloc
&, false_type
)
414 template<typename _Alloc
>
415 inline void __alloc_on_swap(_Alloc
& __one
, _Alloc
& __two
)
417 typedef allocator_traits
<_Alloc
> __traits
;
418 typedef typename
__traits::propagate_on_container_swap __pocs
;
419 __do_alloc_on_swap(__one
, __two
, __pocs());
422 template<typename _Alloc
>
423 class __is_copy_insertable_impl
425 typedef allocator_traits
<_Alloc
> _Traits
;
427 template<typename _Up
, typename
428 = decltype(_Traits::construct(std::declval
<_Alloc
&>(),
429 std::declval
<_Up
*>(),
430 std::declval
<const _Up
&>()))>
434 template<typename _Up
>
439 typedef decltype(_M_select
<typename
_Alloc::value_type
>(0)) type
;
442 // true if _Alloc::value_type is CopyInsertable into containers using _Alloc
443 template<typename _Alloc
>
444 struct __is_copy_insertable
445 : __is_copy_insertable_impl
<_Alloc
>::type
448 // std::allocator<_Tp> just requires CopyConstructible
449 template<typename _Tp
>
450 struct __is_copy_insertable
<allocator
<_Tp
>>
451 : is_copy_constructible
<_Tp
>
454 _GLIBCXX_END_NAMESPACE_VERSION