1 /* Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef COMMON_DEFAULT_INIT_ALLOC_H
19 #define COMMON_DEFAULT_INIT_ALLOC_H
21 #if __cplusplus >= 202002L
22 #include <memory_resource>
27 /* An allocator that default constructs using default-initialization
28 rather than value-initialization. The idea is to use this when you
29 don't want to default construct elements of containers of trivial
30 types using zero-initialization. */
32 /* Mostly as implementation convenience, this is implemented as an
33 adapter that given an allocator A, overrides 'A::construct()'. 'A'
34 defaults to std::allocator<T>. */
38 #if __cplusplus >= 202002L
39 = std::pmr::polymorphic_allocator
<T
>
44 class default_init_allocator
: public A
47 /* Pull in A's ctors. */
50 /* Override rebind. */
54 /* A couple helpers just to make it a bit more readable. */
55 typedef std::allocator_traits
<A
> traits_
;
56 typedef typename
traits_::template rebind_alloc
<U
> alloc_
;
58 /* This is what we're after. */
59 typedef default_init_allocator
<U
, alloc_
> other
;
62 /* Make the base allocator's construct method(s) visible. */
65 /* .. and provide an override/overload for the case of default
66 construction (i.e., no arguments). This is where we construct
69 void construct (U
*ptr
)
70 noexcept (std::is_nothrow_default_constructible
<U
>::value
)
72 ::new ((void *) ptr
) U
; /* default-init */
78 #endif /* COMMON_DEFAULT_INIT_ALLOC_H */