1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SCOPED_ALLOCATOR
11#define _LIBCPP_SCOPED_ALLOCATOR
12
13/*
14    scoped_allocator synopsis
15
16namespace std
17{
18
19template <class OuterAlloc, class... InnerAllocs>
20class scoped_allocator_adaptor : public OuterAlloc
21{
22    typedef allocator_traits<OuterAlloc> OuterTraits; // exposition only
23    scoped_allocator_adaptor<InnerAllocs...> inner;   // exposition only
24public:
25
26    typedef OuterAlloc outer_allocator_type;
27    typedef see below inner_allocator_type;
28
29    typedef typename OuterTraits::value_type value_type;
30    typedef typename OuterTraits::size_type size_type;
31    typedef typename OuterTraits::difference_type difference_type;
32    typedef typename OuterTraits::pointer pointer;
33    typedef typename OuterTraits::const_pointer const_pointer;
34    typedef typename OuterTraits::void_pointer void_pointer;
35    typedef typename OuterTraits::const_void_pointer const_void_pointer;
36
37    typedef see below propagate_on_container_copy_assignment;
38    typedef see below propagate_on_container_move_assignment;
39    typedef see below propagate_on_container_swap;
40    typedef see below is_always_equal;
41
42    template <class Tp>
43        struct rebind
44        {
45            typedef scoped_allocator_adaptor<
46                OuterTraits::template rebind_alloc<Tp>, InnerAllocs...> other;
47        };
48
49    scoped_allocator_adaptor();
50    template <class OuterA2>
51        scoped_allocator_adaptor(OuterA2&& outerAlloc,
52                                 const InnerAllocs&... innerAllocs) noexcept;
53    scoped_allocator_adaptor(const scoped_allocator_adaptor& other) noexcept;
54    scoped_allocator_adaptor(scoped_allocator_adaptor&& other) noexcept;
55    template <class OuterA2>
56        scoped_allocator_adaptor(const scoped_allocator_adaptor<OuterA2, InnerAllocs...>& other) noexcept;
57    template <class OuterA2>
58        scoped_allocator_adaptor(const scoped_allocator_adaptor<OuterA2, InnerAllocs...>&& other) noexcept;
59
60    scoped_allocator_adaptor& operator=(const scoped_allocator_adaptor&) = default;
61    scoped_allocator_adaptor& operator=(scoped_allocator_adaptor&&) = default;
62    ~scoped_allocator_adaptor();
63
64    inner_allocator_type& inner_allocator() noexcept;
65    const inner_allocator_type& inner_allocator() const noexcept;
66
67    outer_allocator_type& outer_allocator() noexcept;
68    const outer_allocator_type& outer_allocator() const noexcept;
69
70    pointer allocate(size_type n);                           // [[nodiscard]] in C++20
71    pointer allocate(size_type n, const_void_pointer hint);  // [[nodiscard]] in C++20
72    void deallocate(pointer p, size_type n) noexcept;
73
74    size_type max_size() const;
75    template <class T, class... Args> void construct(T* p, Args&& args);
76    template <class T1, class T2, class... Args1, class... Args2>
77        void construct(pair<T1, T2>* p, piecewise_construct t, tuple<Args1...> x,
78                       tuple<Args2...> y);
79    template <class T1, class T2>
80        void construct(pair<T1, T2>* p);
81    template <class T1, class T2, class U, class V>
82        void construct(pair<T1, T2>* p, U&& x, V&& y);
83    template <class T1, class T2, class U, class V>
84        void construct(pair<T1, T2>* p, const pair<U, V>& x);
85    template <class T1, class T2, class U, class V>
86        void construct(pair<T1, T2>* p, pair<U, V>&& x);
87    template <class T> void destroy(T* p);
88
89    template <class T> void destroy(T* p) noexcept;
90
91    scoped_allocator_adaptor select_on_container_copy_construction() const noexcept;
92};
93
94template<class OuterAlloc, class... InnerAllocs>
95    scoped_allocator_adaptor(OuterAlloc, InnerAllocs...)
96        -> scoped_allocator_adaptor<OuterAlloc, InnerAllocs...>;
97
98template <class OuterA1, class OuterA2, class... InnerAllocs>
99    bool
100    operator==(const scoped_allocator_adaptor<OuterA1, InnerAllocs...>& a,
101               const scoped_allocator_adaptor<OuterA2, InnerAllocs...>& b) noexcept;
102
103template <class OuterA1, class OuterA2, class... InnerAllocs>
104    bool
105    operator!=(const scoped_allocator_adaptor<OuterA1, InnerAllocs...>& a,
106               const scoped_allocator_adaptor<OuterA2, InnerAllocs...>& b) noexcept; // removed in C++20
107
108}  // std
109
110*/
111
112#include <__assert> // all public C++ headers provide the assertion handler
113#include <__config>
114#include <__memory/allocator_traits.h>
115#include <__memory/uses_allocator_construction.h>
116#include <__type_traits/common_type.h>
117#include <__type_traits/enable_if.h>
118#include <__type_traits/integral_constant.h>
119#include <__type_traits/is_constructible.h>
120#include <__type_traits/remove_reference.h>
121#include <__utility/declval.h>
122#include <__utility/forward.h>
123#include <__utility/move.h>
124#include <__utility/pair.h>
125#include <__utility/piecewise_construct.h>
126#include <tuple>
127#include <version>
128
129#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
130#  pragma GCC system_header
131#endif
132
133_LIBCPP_PUSH_MACROS
134#include <__undef_macros>
135
136_LIBCPP_BEGIN_NAMESPACE_STD
137
138#if !defined(_LIBCPP_CXX03_LANG)
139
140// scoped_allocator_adaptor
141
142template <class... _Allocs>
143class scoped_allocator_adaptor;
144
145template <class... _Allocs>
146struct __get_poc_copy_assignment;
147
148template <class _A0>
149struct __get_poc_copy_assignment<_A0> {
150  static const bool value = allocator_traits<_A0>::propagate_on_container_copy_assignment::value;
151};
152
153template <class _A0, class... _Allocs>
154struct __get_poc_copy_assignment<_A0, _Allocs...> {
155  static const bool value = allocator_traits<_A0>::propagate_on_container_copy_assignment::value ||
156                            __get_poc_copy_assignment<_Allocs...>::value;
157};
158
159template <class... _Allocs>
160struct __get_poc_move_assignment;
161
162template <class _A0>
163struct __get_poc_move_assignment<_A0> {
164  static const bool value = allocator_traits<_A0>::propagate_on_container_move_assignment::value;
165};
166
167template <class _A0, class... _Allocs>
168struct __get_poc_move_assignment<_A0, _Allocs...> {
169  static const bool value = allocator_traits<_A0>::propagate_on_container_move_assignment::value ||
170                            __get_poc_move_assignment<_Allocs...>::value;
171};
172
173template <class... _Allocs>
174struct __get_poc_swap;
175
176template <class _A0>
177struct __get_poc_swap<_A0> {
178  static const bool value = allocator_traits<_A0>::propagate_on_container_swap::value;
179};
180
181template <class _A0, class... _Allocs>
182struct __get_poc_swap<_A0, _Allocs...> {
183  static const bool value =
184      allocator_traits<_A0>::propagate_on_container_swap::value || __get_poc_swap<_Allocs...>::value;
185};
186
187template <class... _Allocs>
188struct __get_is_always_equal;
189
190template <class _A0>
191struct __get_is_always_equal<_A0> {
192  static const bool value = allocator_traits<_A0>::is_always_equal::value;
193};
194
195template <class _A0, class... _Allocs>
196struct __get_is_always_equal<_A0, _Allocs...> {
197  static const bool value = allocator_traits<_A0>::is_always_equal::value && __get_is_always_equal<_Allocs...>::value;
198};
199
200template <class... _Allocs>
201class __scoped_allocator_storage;
202
203template <class _OuterAlloc, class... _InnerAllocs>
204class __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...> : public _OuterAlloc {
205  typedef _OuterAlloc outer_allocator_type;
206
207protected:
208  typedef scoped_allocator_adaptor<_InnerAllocs...> inner_allocator_type;
209
210private:
211  inner_allocator_type __inner_;
212
213protected:
214  _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage() _NOEXCEPT {}
215
216  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
217  _LIBCPP_HIDE_FROM_ABI
218  __scoped_allocator_storage(_OuterA2&& __outer_alloc, const _InnerAllocs&... __inner_allocs) _NOEXCEPT
219      : outer_allocator_type(std::forward<_OuterA2>(__outer_alloc)),
220        __inner_(__inner_allocs...) {}
221
222  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, const _OuterA2&>::value, int> = 0>
223  _LIBCPP_HIDE_FROM_ABI
224  __scoped_allocator_storage(const __scoped_allocator_storage<_OuterA2, _InnerAllocs...>& __other) _NOEXCEPT
225      : outer_allocator_type(__other.outer_allocator()),
226        __inner_(__other.inner_allocator()) {}
227
228  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
229  _LIBCPP_HIDE_FROM_ABI
230  __scoped_allocator_storage(__scoped_allocator_storage<_OuterA2, _InnerAllocs...>&& __other) _NOEXCEPT
231      : outer_allocator_type(std::move(__other.outer_allocator())),
232        __inner_(std::move(__other.inner_allocator())) {}
233
234  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
235  _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(_OuterA2&& __o, const inner_allocator_type& __i) _NOEXCEPT
236      : outer_allocator_type(std::forward<_OuterA2>(__o)),
237        __inner_(__i) {}
238
239  _LIBCPP_HIDE_FROM_ABI inner_allocator_type& inner_allocator() _NOEXCEPT { return __inner_; }
240  _LIBCPP_HIDE_FROM_ABI const inner_allocator_type& inner_allocator() const _NOEXCEPT { return __inner_; }
241
242  _LIBCPP_HIDE_FROM_ABI outer_allocator_type& outer_allocator() _NOEXCEPT {
243    return static_cast<outer_allocator_type&>(*this);
244  }
245  _LIBCPP_HIDE_FROM_ABI const outer_allocator_type& outer_allocator() const _NOEXCEPT {
246    return static_cast<const outer_allocator_type&>(*this);
247  }
248
249  scoped_allocator_adaptor<outer_allocator_type, _InnerAllocs...> _LIBCPP_HIDE_FROM_ABI
250  select_on_container_copy_construction() const _NOEXCEPT {
251    return scoped_allocator_adaptor<outer_allocator_type, _InnerAllocs...>(
252        allocator_traits<outer_allocator_type>::select_on_container_copy_construction(outer_allocator()),
253        allocator_traits<inner_allocator_type>::select_on_container_copy_construction(inner_allocator()));
254  }
255
256  template <class...>
257  friend class __scoped_allocator_storage;
258};
259
260template <class _OuterAlloc>
261class __scoped_allocator_storage<_OuterAlloc> : public _OuterAlloc {
262  typedef _OuterAlloc outer_allocator_type;
263
264protected:
265  typedef scoped_allocator_adaptor<_OuterAlloc> inner_allocator_type;
266
267  _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage() _NOEXCEPT {}
268
269  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
270  _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(_OuterA2&& __outer_alloc) _NOEXCEPT
271      : outer_allocator_type(std::forward<_OuterA2>(__outer_alloc)) {}
272
273  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, const _OuterA2&>::value, int> = 0>
274  _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(const __scoped_allocator_storage<_OuterA2>& __other) _NOEXCEPT
275      : outer_allocator_type(__other.outer_allocator()) {}
276
277  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
278  _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(__scoped_allocator_storage<_OuterA2>&& __other) _NOEXCEPT
279      : outer_allocator_type(std::move(__other.outer_allocator())) {}
280
281  _LIBCPP_HIDE_FROM_ABI inner_allocator_type& inner_allocator() _NOEXCEPT {
282    return static_cast<inner_allocator_type&>(*this);
283  }
284  _LIBCPP_HIDE_FROM_ABI const inner_allocator_type& inner_allocator() const _NOEXCEPT {
285    return static_cast<const inner_allocator_type&>(*this);
286  }
287
288  _LIBCPP_HIDE_FROM_ABI outer_allocator_type& outer_allocator() _NOEXCEPT {
289    return static_cast<outer_allocator_type&>(*this);
290  }
291  _LIBCPP_HIDE_FROM_ABI const outer_allocator_type& outer_allocator() const _NOEXCEPT {
292    return static_cast<const outer_allocator_type&>(*this);
293  }
294
295  _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor<outer_allocator_type>
296  select_on_container_copy_construction() const _NOEXCEPT {
297    return scoped_allocator_adaptor<outer_allocator_type>(
298        allocator_traits<outer_allocator_type>::select_on_container_copy_construction(outer_allocator()));
299  }
300
301  __scoped_allocator_storage(const outer_allocator_type& __o, const inner_allocator_type& __i) _NOEXCEPT;
302
303  template <class...>
304  friend class __scoped_allocator_storage;
305};
306
307// __outermost
308
309template <class _Alloc>
310decltype(std::declval<_Alloc>().outer_allocator(), true_type()) __has_outer_allocator_test(_Alloc&& __a);
311
312template <class _Alloc>
313false_type __has_outer_allocator_test(const volatile _Alloc& __a);
314
315template <class _Alloc>
316struct __has_outer_allocator
317    : public common_type< decltype(std::__has_outer_allocator_test(std::declval<_Alloc&>())) >::type {};
318
319template <class _Alloc, bool = __has_outer_allocator<_Alloc>::value>
320struct __outermost {
321  typedef _Alloc type;
322  _LIBCPP_HIDE_FROM_ABI type& operator()(type& __a) const _NOEXCEPT { return __a; }
323};
324
325template <class _Alloc>
326struct __outermost<_Alloc, true> {
327  typedef __libcpp_remove_reference_t< decltype(std::declval<_Alloc>().outer_allocator()) > _OuterAlloc;
328  typedef typename __outermost<_OuterAlloc>::type type;
329  _LIBCPP_HIDE_FROM_ABI type& operator()(_Alloc& __a) const _NOEXCEPT {
330    return __outermost<_OuterAlloc>()(__a.outer_allocator());
331  }
332};
333
334template <class _OuterAlloc, class... _InnerAllocs>
335class _LIBCPP_TEMPLATE_VIS scoped_allocator_adaptor<_OuterAlloc, _InnerAllocs...>
336    : public __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...> {
337  typedef __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...> base;
338  typedef allocator_traits<_OuterAlloc> _OuterTraits;
339
340public:
341  typedef _OuterAlloc outer_allocator_type;
342  typedef typename base::inner_allocator_type inner_allocator_type;
343  typedef typename _OuterTraits::size_type size_type;
344  typedef typename _OuterTraits::difference_type difference_type;
345  typedef typename _OuterTraits::pointer pointer;
346  typedef typename _OuterTraits::const_pointer const_pointer;
347  typedef typename _OuterTraits::void_pointer void_pointer;
348  typedef typename _OuterTraits::const_void_pointer const_void_pointer;
349
350  typedef integral_constant< bool, __get_poc_copy_assignment<outer_allocator_type, _InnerAllocs...>::value >
351      propagate_on_container_copy_assignment;
352  typedef integral_constant< bool, __get_poc_move_assignment<outer_allocator_type, _InnerAllocs...>::value >
353      propagate_on_container_move_assignment;
354  typedef integral_constant< bool, __get_poc_swap<outer_allocator_type, _InnerAllocs...>::value >
355      propagate_on_container_swap;
356  typedef integral_constant< bool, __get_is_always_equal<outer_allocator_type, _InnerAllocs...>::value >
357      is_always_equal;
358
359  template <class _Tp>
360  struct rebind {
361    typedef scoped_allocator_adaptor< typename _OuterTraits::template rebind_alloc<_Tp>, _InnerAllocs... > other;
362  };
363
364  _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor() _NOEXCEPT {}
365  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
366  _LIBCPP_HIDE_FROM_ABI
367  scoped_allocator_adaptor(_OuterA2&& __outer_alloc, const _InnerAllocs&... __inner_allocs) _NOEXCEPT
368      : base(std::forward<_OuterA2>(__outer_alloc), __inner_allocs...) {}
369  // scoped_allocator_adaptor(const scoped_allocator_adaptor& __other) = default;
370  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, const _OuterA2&>::value, int> = 0>
371  _LIBCPP_HIDE_FROM_ABI
372  scoped_allocator_adaptor(const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __other) _NOEXCEPT
373      : base(__other) {}
374  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
375  _LIBCPP_HIDE_FROM_ABI
376  scoped_allocator_adaptor(scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>&& __other) _NOEXCEPT
377      : base(std::move(__other)) {}
378
379  // scoped_allocator_adaptor& operator=(const scoped_allocator_adaptor&) = default;
380  // scoped_allocator_adaptor& operator=(scoped_allocator_adaptor&&) = default;
381  // ~scoped_allocator_adaptor() = default;
382
383  _LIBCPP_HIDE_FROM_ABI inner_allocator_type& inner_allocator() _NOEXCEPT { return base::inner_allocator(); }
384  _LIBCPP_HIDE_FROM_ABI const inner_allocator_type& inner_allocator() const _NOEXCEPT {
385    return base::inner_allocator();
386  }
387
388  _LIBCPP_HIDE_FROM_ABI outer_allocator_type& outer_allocator() _NOEXCEPT { return base::outer_allocator(); }
389  _LIBCPP_HIDE_FROM_ABI const outer_allocator_type& outer_allocator() const _NOEXCEPT {
390    return base::outer_allocator();
391  }
392
393  _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI pointer allocate(size_type __n) {
394    return allocator_traits<outer_allocator_type>::allocate(outer_allocator(), __n);
395  }
396  _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI pointer allocate(size_type __n, const_void_pointer __hint) {
397    return allocator_traits<outer_allocator_type>::allocate(outer_allocator(), __n, __hint);
398  }
399
400  _LIBCPP_HIDE_FROM_ABI void deallocate(pointer __p, size_type __n) _NOEXCEPT {
401    allocator_traits<outer_allocator_type>::deallocate(outer_allocator(), __p, __n);
402  }
403
404  _LIBCPP_HIDE_FROM_ABI size_type max_size() const {
405    return allocator_traits<outer_allocator_type>::max_size(outer_allocator());
406  }
407
408#  if _LIBCPP_STD_VER >= 20
409  template <class _Type, class... _Args>
410  _LIBCPP_HIDE_FROM_ABI void construct(_Type* __ptr, _Args&&... __args) {
411    using _OM = __outermost<outer_allocator_type>;
412    std::apply(
413        [__ptr, this](auto&&... __newargs) {
414          allocator_traits<typename _OM::type>::construct(
415              _OM()(outer_allocator()), __ptr, std::forward<decltype(__newargs)>(__newargs)...);
416        },
417        std::uses_allocator_construction_args<_Type>(inner_allocator(), std::forward<_Args>(__args)...));
418  }
419#  else
420  template <class _Tp, class... _Args>
421  _LIBCPP_HIDE_FROM_ABI void construct(_Tp* __p, _Args&&... __args) {
422    __construct(__uses_alloc_ctor<_Tp, inner_allocator_type&, _Args...>(), __p, std::forward<_Args>(__args)...);
423  }
424
425  template <class _T1, class _T2, class... _Args1, class... _Args2>
426  _LIBCPP_HIDE_FROM_ABI void
427  construct(pair<_T1, _T2>* __p, piecewise_construct_t, tuple<_Args1...> __x, tuple<_Args2...> __y) {
428    typedef __outermost<outer_allocator_type> _OM;
429    allocator_traits<typename _OM::type>::construct(
430        _OM()(outer_allocator()),
431        __p,
432        piecewise_construct,
433        __transform_tuple(typename __uses_alloc_ctor< _T1, inner_allocator_type&, _Args1... >::type(),
434                          std::move(__x),
435                          typename __make_tuple_indices<sizeof...(_Args1)>::type{}),
436        __transform_tuple(typename __uses_alloc_ctor< _T2, inner_allocator_type&, _Args2... >::type(),
437                          std::move(__y),
438                          typename __make_tuple_indices<sizeof...(_Args2)>::type{}));
439  }
440
441  template <class _T1, class _T2>
442  _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p) {
443    construct(__p, piecewise_construct, tuple<>{}, tuple<>{});
444  }
445
446  template <class _T1, class _T2, class _Up, class _Vp>
447  _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, _Up&& __x, _Vp&& __y) {
448    construct(__p,
449              piecewise_construct,
450              std::forward_as_tuple(std::forward<_Up>(__x)),
451              std::forward_as_tuple(std::forward<_Vp>(__y)));
452  }
453
454  template <class _T1, class _T2, class _Up, class _Vp>
455  _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x) {
456    construct(__p, piecewise_construct, std::forward_as_tuple(__x.first), std::forward_as_tuple(__x.second));
457  }
458
459  template <class _T1, class _T2, class _Up, class _Vp>
460  _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x) {
461    construct(__p,
462              piecewise_construct,
463              std::forward_as_tuple(std::forward<_Up>(__x.first)),
464              std::forward_as_tuple(std::forward<_Vp>(__x.second)));
465  }
466#  endif
467
468  template <class _Tp>
469  _LIBCPP_HIDE_FROM_ABI void destroy(_Tp* __p) {
470    typedef __outermost<outer_allocator_type> _OM;
471    allocator_traits<typename _OM::type>::destroy(_OM()(outer_allocator()), __p);
472  }
473
474  _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor select_on_container_copy_construction() const _NOEXCEPT {
475    return base::select_on_container_copy_construction();
476  }
477
478private:
479  template <class _OuterA2, __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
480  _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor(_OuterA2&& __o, const inner_allocator_type& __i) _NOEXCEPT
481      : base(std::forward<_OuterA2>(__o), __i) {}
482
483  template <class _Tp, class... _Args>
484  _LIBCPP_HIDE_FROM_ABI void __construct(integral_constant<int, 0>, _Tp* __p, _Args&&... __args) {
485    typedef __outermost<outer_allocator_type> _OM;
486    allocator_traits<typename _OM::type>::construct(_OM()(outer_allocator()), __p, std::forward<_Args>(__args)...);
487  }
488
489  template <class _Tp, class... _Args>
490  _LIBCPP_HIDE_FROM_ABI void __construct(integral_constant<int, 1>, _Tp* __p, _Args&&... __args) {
491    typedef __outermost<outer_allocator_type> _OM;
492    allocator_traits<typename _OM::type>::construct(
493        _OM()(outer_allocator()), __p, allocator_arg, inner_allocator(), std::forward<_Args>(__args)...);
494  }
495
496  template <class _Tp, class... _Args>
497  _LIBCPP_HIDE_FROM_ABI void __construct(integral_constant<int, 2>, _Tp* __p, _Args&&... __args) {
498    typedef __outermost<outer_allocator_type> _OM;
499    allocator_traits<typename _OM::type>::construct(
500        _OM()(outer_allocator()), __p, std::forward<_Args>(__args)..., inner_allocator());
501  }
502
503  template <class... _Args, size_t... _Idx>
504  _LIBCPP_HIDE_FROM_ABI tuple<_Args&&...>
505  __transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t, __tuple_indices<_Idx...>) {
506    return std::forward_as_tuple(std::get<_Idx>(std::move(__t))...);
507  }
508
509  template <class... _Args, size_t... _Idx>
510  _LIBCPP_HIDE_FROM_ABI tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
511  __transform_tuple(integral_constant<int, 1>, tuple<_Args...>&& __t, __tuple_indices<_Idx...>) {
512    using _Tup = tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>;
513    return _Tup(allocator_arg, inner_allocator(), std::get<_Idx>(std::move(__t))...);
514  }
515
516  template <class... _Args, size_t... _Idx>
517  _LIBCPP_HIDE_FROM_ABI tuple<_Args&&..., inner_allocator_type&>
518  __transform_tuple(integral_constant<int, 2>, tuple<_Args...>&& __t, __tuple_indices<_Idx...>) {
519    using _Tup = tuple<_Args&&..., inner_allocator_type&>;
520    return _Tup(std::get<_Idx>(std::move(__t))..., inner_allocator());
521  }
522
523  template <class...>
524  friend class __scoped_allocator_storage;
525};
526
527#  if _LIBCPP_STD_VER >= 17
528template <class _OuterAlloc, class... _InnerAllocs>
529scoped_allocator_adaptor(_OuterAlloc, _InnerAllocs...) -> scoped_allocator_adaptor<_OuterAlloc, _InnerAllocs...>;
530#  endif
531
532template <class _OuterA1, class _OuterA2>
533inline _LIBCPP_HIDE_FROM_ABI bool
534operator==(const scoped_allocator_adaptor<_OuterA1>& __a, const scoped_allocator_adaptor<_OuterA2>& __b) _NOEXCEPT {
535  return __a.outer_allocator() == __b.outer_allocator();
536}
537
538template <class _OuterA1, class _OuterA2, class _InnerA0, class... _InnerAllocs>
539inline _LIBCPP_HIDE_FROM_ABI bool
540operator==(const scoped_allocator_adaptor<_OuterA1, _InnerA0, _InnerAllocs...>& __a,
541           const scoped_allocator_adaptor<_OuterA2, _InnerA0, _InnerAllocs...>& __b) _NOEXCEPT {
542  return __a.outer_allocator() == __b.outer_allocator() && __a.inner_allocator() == __b.inner_allocator();
543}
544
545#  if _LIBCPP_STD_VER <= 17
546
547template <class _OuterA1, class _OuterA2, class... _InnerAllocs>
548inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const scoped_allocator_adaptor<_OuterA1, _InnerAllocs...>& __a,
549                                             const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __b) _NOEXCEPT {
550  return !(__a == __b);
551}
552
553#  endif // _LIBCPP_STD_VER <= 17
554
555#endif // !defined(_LIBCPP_CXX03_LANG)
556
557_LIBCPP_END_NAMESPACE_STD
558
559_LIBCPP_POP_MACROS
560
561#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
562#  include <atomic>
563#  include <climits>
564#  include <concepts>
565#  include <cstring>
566#  include <ctime>
567#  include <iterator>
568#  include <memory>
569#  include <ratio>
570#  include <stdexcept>
571#  include <type_traits>
572#  include <variant>
573#endif
574
575#endif // _LIBCPP_SCOPED_ALLOCATOR
576