libstdc++
uses_allocator_args.h
Go to the documentation of this file.
1// Utility functions for uses-allocator construction -*- C++ -*-
2
3// Copyright (C) 2019-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 include/bits/uses_allocator_args.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 _USES_ALLOCATOR_ARGS
31#define _USES_ALLOCATOR_ARGS 1
32
33#pragma GCC system_header
34
35#if __cplusplus > 201703L && __cpp_concepts
36
37#include <new> // for placement operator new
38#include <tuple> // for tuple, make_tuple, make_from_tuple
39#include <bits/stl_construct.h> // construct_at
40#include <bits/stl_pair.h> // pair
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 template<typename _Tp>
47 concept _Std_pair = __is_pair<_Tp>;
48
49/** @addtogroup allocators
50 * @{
51 */
52
53// Not specified by C++20, used internally
54#define __cpp_lib_make_obj_using_allocator 201811L
55
56 template<typename _Tp, typename _Alloc, typename... _Args>
57 constexpr auto
58 uses_allocator_construction_args(const _Alloc& __a,
59 _Args&&... __args) noexcept
60 requires (! _Std_pair<_Tp>)
61 {
62 if constexpr (uses_allocator_v<remove_cv_t<_Tp>, _Alloc>)
63 {
64 if constexpr (is_constructible_v<_Tp, allocator_arg_t,
65 const _Alloc&, _Args...>)
66 {
67 return tuple<allocator_arg_t, const _Alloc&, _Args&&...>(
68 allocator_arg, __a, std::forward<_Args>(__args)...);
69 }
70 else
71 {
72 static_assert(is_constructible_v<_Tp, _Args..., const _Alloc&>,
73 "construction with an allocator must be possible"
74 " if uses_allocator is true");
75
76 return tuple<_Args&&..., const _Alloc&>(
77 std::forward<_Args>(__args)..., __a);
78 }
79 }
80 else
81 {
82 static_assert(is_constructible_v<_Tp, _Args...>);
83
84 return tuple<_Args&&...>(std::forward<_Args>(__args)...);
85 }
86 }
87
88 template<_Std_pair _Tp, typename _Alloc, typename _Tuple1, typename _Tuple2>
89 constexpr auto
90 uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t,
91 _Tuple1&& __x, _Tuple2&& __y) noexcept;
92
93 template<_Std_pair _Tp, typename _Alloc>
94 constexpr auto
95 uses_allocator_construction_args(const _Alloc&) noexcept;
96
97 template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
98 constexpr auto
99 uses_allocator_construction_args(const _Alloc&, _Up&&, _Vp&&) noexcept;
100
101 template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
102 constexpr auto
103 uses_allocator_construction_args(const _Alloc&,
104 const pair<_Up, _Vp>&) noexcept;
105
106 template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
107 constexpr auto
108 uses_allocator_construction_args(const _Alloc&, pair<_Up, _Vp>&&) noexcept;
109
110 template<_Std_pair _Tp, typename _Alloc, typename _Tuple1, typename _Tuple2>
111 constexpr auto
112 uses_allocator_construction_args(const _Alloc& __a, piecewise_construct_t,
113 _Tuple1&& __x, _Tuple2&& __y) noexcept
114 {
115 using _Tp1 = typename _Tp::first_type;
116 using _Tp2 = typename _Tp::second_type;
117
118 return std::make_tuple(piecewise_construct,
119 std::apply([&__a](auto&&... __args1) {
120 return std::uses_allocator_construction_args<_Tp1>(
121 __a, std::forward<decltype(__args1)>(__args1)...);
122 }, std::forward<_Tuple1>(__x)),
123 std::apply([&__a](auto&&... __args2) {
124 return std::uses_allocator_construction_args<_Tp2>(
125 __a, std::forward<decltype(__args2)>(__args2)...);
126 }, std::forward<_Tuple2>(__y)));
127 }
128
129 template<_Std_pair _Tp, typename _Alloc>
130 constexpr auto
131 uses_allocator_construction_args(const _Alloc& __a) noexcept
132 {
133 using _Tp1 = typename _Tp::first_type;
134 using _Tp2 = typename _Tp::second_type;
135
136 return std::make_tuple(piecewise_construct,
137 std::uses_allocator_construction_args<_Tp1>(__a),
138 std::uses_allocator_construction_args<_Tp2>(__a));
139 }
140
141 template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
142 constexpr auto
143 uses_allocator_construction_args(const _Alloc& __a, _Up&& __u, _Vp&& __v)
144 noexcept
145 {
146 using _Tp1 = typename _Tp::first_type;
147 using _Tp2 = typename _Tp::second_type;
148
149 return std::make_tuple(piecewise_construct,
150 std::uses_allocator_construction_args<_Tp1>(__a,
151 std::forward<_Up>(__u)),
152 std::uses_allocator_construction_args<_Tp2>(__a,
153 std::forward<_Vp>(__v)));
154 }
155
156 template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
157 constexpr auto
158 uses_allocator_construction_args(const _Alloc& __a,
159 const pair<_Up, _Vp>& __pr) noexcept
160 {
161 using _Tp1 = typename _Tp::first_type;
162 using _Tp2 = typename _Tp::second_type;
163
164 return std::make_tuple(piecewise_construct,
165 std::uses_allocator_construction_args<_Tp1>(__a, __pr.first),
166 std::uses_allocator_construction_args<_Tp2>(__a, __pr.second));
167 }
168
169 template<_Std_pair _Tp, typename _Alloc, typename _Up, typename _Vp>
170 constexpr auto
171 uses_allocator_construction_args(const _Alloc& __a,
172 pair<_Up, _Vp>&& __pr) noexcept
173 {
174 using _Tp1 = typename _Tp::first_type;
175 using _Tp2 = typename _Tp::second_type;
176
177 // _GLIBCXX_RESOLVE_LIB_DEFECTS
178 // 3527. uses_allocator_construction_args handles rvalue pairs
179 // of rvalue references incorrectly
180 return std::make_tuple(piecewise_construct,
181 std::uses_allocator_construction_args<_Tp1>(__a,
182 std::get<0>(std::move(__pr))),
183 std::uses_allocator_construction_args<_Tp2>(__a,
184 std::get<1>(std::move(__pr))));
185 }
186
187 template<typename _Tp, typename _Alloc, typename... _Args>
188 constexpr _Tp
189 make_obj_using_allocator(const _Alloc& __a, _Args&&... __args)
190 {
191 return std::make_from_tuple<_Tp>(
192 std::uses_allocator_construction_args<_Tp>(__a,
193 std::forward<_Args>(__args)...));
194 }
195
196 template<typename _Tp, typename _Alloc, typename... _Args>
197 constexpr _Tp*
198 uninitialized_construct_using_allocator(_Tp* __p, const _Alloc& __a,
199 _Args&&... __args)
200 {
201 return std::apply([&](auto&&... __xs) {
202 return std::construct_at(__p, std::forward<decltype(__xs)>(__xs)...);
203 }, std::uses_allocator_construction_args<_Tp>(__a,
204 std::forward<_Args>(__args)...));
205 }
206/// @}
207_GLIBCXX_END_NAMESPACE_VERSION
208} // namespace std
209#endif // C++20
210#endif // _USES_ALLOCATOR_ARGS
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition: stl_pair.h:83
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:77
ISO C++ entities toplevel namespace is std.