xref: /freebsd-src/contrib/llvm-project/libcxx/include/scoped_allocator (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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> struct __get_poc_copy_assignment;
146
147template <class _A0>
148struct __get_poc_copy_assignment<_A0>
149{
150    static const bool value = allocator_traits<_A0>::
151                              propagate_on_container_copy_assignment::value;
152};
153
154template <class _A0, class ..._Allocs>
155struct __get_poc_copy_assignment<_A0, _Allocs...>
156{
157    static const bool value =
158        allocator_traits<_A0>::propagate_on_container_copy_assignment::value ||
159        __get_poc_copy_assignment<_Allocs...>::value;
160};
161
162template <class ..._Allocs> struct __get_poc_move_assignment;
163
164template <class _A0>
165struct __get_poc_move_assignment<_A0>
166{
167    static const bool value = allocator_traits<_A0>::
168                              propagate_on_container_move_assignment::value;
169};
170
171template <class _A0, class ..._Allocs>
172struct __get_poc_move_assignment<_A0, _Allocs...>
173{
174    static const bool value =
175        allocator_traits<_A0>::propagate_on_container_move_assignment::value ||
176        __get_poc_move_assignment<_Allocs...>::value;
177};
178
179template <class ..._Allocs> struct __get_poc_swap;
180
181template <class _A0>
182struct __get_poc_swap<_A0>
183{
184    static const bool value = allocator_traits<_A0>::
185                              propagate_on_container_swap::value;
186};
187
188template <class _A0, class ..._Allocs>
189struct __get_poc_swap<_A0, _Allocs...>
190{
191    static const bool value =
192        allocator_traits<_A0>::propagate_on_container_swap::value ||
193        __get_poc_swap<_Allocs...>::value;
194};
195
196template <class ..._Allocs> struct __get_is_always_equal;
197
198template <class _A0>
199struct __get_is_always_equal<_A0>
200{
201    static const bool value = allocator_traits<_A0>::is_always_equal::value;
202};
203
204template <class _A0, class ..._Allocs>
205struct __get_is_always_equal<_A0, _Allocs...>
206{
207    static const bool value =
208        allocator_traits<_A0>::is_always_equal::value &&
209        __get_is_always_equal<_Allocs...>::value;
210};
211
212template <class ..._Allocs>
213class __scoped_allocator_storage;
214
215template <class _OuterAlloc, class... _InnerAllocs>
216class __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...>
217    : public _OuterAlloc
218{
219    typedef _OuterAlloc outer_allocator_type;
220protected:
221    typedef scoped_allocator_adaptor<_InnerAllocs...> inner_allocator_type;
222
223private:
224    inner_allocator_type __inner_;
225
226protected:
227
228    _LIBCPP_HIDE_FROM_ABI
229    __scoped_allocator_storage() _NOEXCEPT {}
230
231    template <class _OuterA2,
232              __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
233        _LIBCPP_HIDE_FROM_ABI
234        __scoped_allocator_storage(_OuterA2&& __outer_alloc,
235                                   const _InnerAllocs& ...__inner_allocs) _NOEXCEPT
236            : outer_allocator_type(std::forward<_OuterA2>(__outer_alloc)),
237              __inner_(__inner_allocs...) {}
238
239    template <class _OuterA2,
240              __enable_if_t<is_constructible<outer_allocator_type, const _OuterA2&>::value, int> = 0>
241        _LIBCPP_HIDE_FROM_ABI
242        __scoped_allocator_storage(
243            const __scoped_allocator_storage<_OuterA2, _InnerAllocs...>& __other) _NOEXCEPT
244            : outer_allocator_type(__other.outer_allocator()),
245              __inner_(__other.inner_allocator()) {}
246
247    template <class _OuterA2,
248              __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
249        _LIBCPP_HIDE_FROM_ABI
250        __scoped_allocator_storage(
251            __scoped_allocator_storage<_OuterA2, _InnerAllocs...>&& __other) _NOEXCEPT
252            : outer_allocator_type(std::move(__other.outer_allocator())),
253              __inner_(std::move(__other.inner_allocator())) {}
254
255    template <class _OuterA2,
256              __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
257        _LIBCPP_HIDE_FROM_ABI
258        __scoped_allocator_storage(_OuterA2&& __o,
259                                   const inner_allocator_type& __i) _NOEXCEPT
260            : outer_allocator_type(std::forward<_OuterA2>(__o)),
261              __inner_(__i)
262        {
263        }
264
265    _LIBCPP_HIDE_FROM_ABI
266    inner_allocator_type& inner_allocator() _NOEXCEPT             {return __inner_;}
267    _LIBCPP_HIDE_FROM_ABI
268    const inner_allocator_type& inner_allocator() const _NOEXCEPT {return __inner_;}
269
270    _LIBCPP_HIDE_FROM_ABI
271    outer_allocator_type& outer_allocator() _NOEXCEPT
272        {return static_cast<outer_allocator_type&>(*this);}
273    _LIBCPP_HIDE_FROM_ABI
274    const outer_allocator_type& outer_allocator() const _NOEXCEPT
275        {return static_cast<const outer_allocator_type&>(*this);}
276
277    scoped_allocator_adaptor<outer_allocator_type, _InnerAllocs...>
278    _LIBCPP_HIDE_FROM_ABI
279    select_on_container_copy_construction() const _NOEXCEPT
280        {
281            return scoped_allocator_adaptor<outer_allocator_type, _InnerAllocs...>
282            (
283                allocator_traits<outer_allocator_type>::
284                    select_on_container_copy_construction(outer_allocator()),
285                allocator_traits<inner_allocator_type>::
286                    select_on_container_copy_construction(inner_allocator())
287            );
288        }
289
290    template <class...> friend class __scoped_allocator_storage;
291};
292
293template <class _OuterAlloc>
294class __scoped_allocator_storage<_OuterAlloc>
295    : public _OuterAlloc
296{
297    typedef _OuterAlloc outer_allocator_type;
298protected:
299    typedef scoped_allocator_adaptor<_OuterAlloc> inner_allocator_type;
300
301    _LIBCPP_HIDE_FROM_ABI
302    __scoped_allocator_storage() _NOEXCEPT {}
303
304    template <class _OuterA2,
305              __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
306        _LIBCPP_HIDE_FROM_ABI
307        __scoped_allocator_storage(_OuterA2&& __outer_alloc) _NOEXCEPT
308            : outer_allocator_type(std::forward<_OuterA2>(__outer_alloc)) {}
309
310    template <class _OuterA2,
311              __enable_if_t<is_constructible<outer_allocator_type, const _OuterA2&>::value, int> = 0>
312        _LIBCPP_HIDE_FROM_ABI
313        __scoped_allocator_storage(
314            const __scoped_allocator_storage<_OuterA2>& __other) _NOEXCEPT
315            : outer_allocator_type(__other.outer_allocator()) {}
316
317    template <class _OuterA2,
318              __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
319        _LIBCPP_HIDE_FROM_ABI
320        __scoped_allocator_storage(
321            __scoped_allocator_storage<_OuterA2>&& __other) _NOEXCEPT
322            : outer_allocator_type(std::move(__other.outer_allocator())) {}
323
324    _LIBCPP_HIDE_FROM_ABI
325    inner_allocator_type& inner_allocator() _NOEXCEPT
326        {return static_cast<inner_allocator_type&>(*this);}
327    _LIBCPP_HIDE_FROM_ABI
328    const inner_allocator_type& inner_allocator() const _NOEXCEPT
329        {return static_cast<const inner_allocator_type&>(*this);}
330
331    _LIBCPP_HIDE_FROM_ABI
332    outer_allocator_type& outer_allocator() _NOEXCEPT
333        {return static_cast<outer_allocator_type&>(*this);}
334    _LIBCPP_HIDE_FROM_ABI
335    const outer_allocator_type& outer_allocator() const _NOEXCEPT
336        {return static_cast<const outer_allocator_type&>(*this);}
337
338    _LIBCPP_HIDE_FROM_ABI
339    scoped_allocator_adaptor<outer_allocator_type>
340    select_on_container_copy_construction() const _NOEXCEPT
341        {return scoped_allocator_adaptor<outer_allocator_type>(
342            allocator_traits<outer_allocator_type>::
343                select_on_container_copy_construction(outer_allocator())
344        );}
345
346    __scoped_allocator_storage(const outer_allocator_type& __o,
347                               const inner_allocator_type& __i) _NOEXCEPT;
348
349    template <class...> friend class __scoped_allocator_storage;
350};
351
352// __outermost
353
354template <class _Alloc>
355decltype(std::declval<_Alloc>().outer_allocator(), true_type())
356__has_outer_allocator_test(_Alloc&& __a);
357
358template <class _Alloc>
359false_type
360__has_outer_allocator_test(const volatile _Alloc& __a);
361
362template <class _Alloc>
363struct __has_outer_allocator
364    : public common_type
365             <
366                 decltype(std::__has_outer_allocator_test(std::declval<_Alloc&>()))
367             >::type
368{
369};
370
371template <class _Alloc, bool = __has_outer_allocator<_Alloc>::value>
372struct __outermost
373{
374    typedef _Alloc type;
375    _LIBCPP_HIDE_FROM_ABI
376    type& operator()(type& __a) const _NOEXCEPT {return __a;}
377};
378
379template <class _Alloc>
380struct __outermost<_Alloc, true>
381{
382    typedef __libcpp_remove_reference_t
383                     <
384                        decltype(std::declval<_Alloc>().outer_allocator())
385                     >                                    _OuterAlloc;
386    typedef typename __outermost<_OuterAlloc>::type             type;
387    _LIBCPP_HIDE_FROM_ABI
388    type& operator()(_Alloc& __a) const _NOEXCEPT
389        {return __outermost<_OuterAlloc>()(__a.outer_allocator());}
390};
391
392template <class _OuterAlloc, class... _InnerAllocs>
393class _LIBCPP_TEMPLATE_VIS scoped_allocator_adaptor<_OuterAlloc, _InnerAllocs...>
394    : public __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...>
395{
396    typedef __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...> base;
397    typedef allocator_traits<_OuterAlloc>             _OuterTraits;
398public:
399    typedef _OuterAlloc                               outer_allocator_type;
400    typedef typename base::inner_allocator_type       inner_allocator_type;
401    typedef typename _OuterTraits::size_type          size_type;
402    typedef typename _OuterTraits::difference_type    difference_type;
403    typedef typename _OuterTraits::pointer            pointer;
404    typedef typename _OuterTraits::const_pointer      const_pointer;
405    typedef typename _OuterTraits::void_pointer       void_pointer;
406    typedef typename _OuterTraits::const_void_pointer const_void_pointer;
407
408    typedef integral_constant
409            <
410                bool,
411                __get_poc_copy_assignment<outer_allocator_type,
412                                          _InnerAllocs...>::value
413            > propagate_on_container_copy_assignment;
414    typedef integral_constant
415            <
416                bool,
417                __get_poc_move_assignment<outer_allocator_type,
418                                          _InnerAllocs...>::value
419            > propagate_on_container_move_assignment;
420    typedef integral_constant
421            <
422                bool,
423                __get_poc_swap<outer_allocator_type, _InnerAllocs...>::value
424            > propagate_on_container_swap;
425    typedef integral_constant
426            <
427                bool,
428                __get_is_always_equal<outer_allocator_type, _InnerAllocs...>::value
429            > is_always_equal;
430
431    template <class _Tp>
432    struct rebind
433    {
434        typedef scoped_allocator_adaptor
435        <
436            typename _OuterTraits::template rebind_alloc<_Tp>, _InnerAllocs...
437        > other;
438    };
439
440    _LIBCPP_HIDE_FROM_ABI
441    scoped_allocator_adaptor() _NOEXCEPT {}
442    template <class _OuterA2,
443              __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
444        _LIBCPP_HIDE_FROM_ABI
445        scoped_allocator_adaptor(_OuterA2&& __outer_alloc,
446                                 const _InnerAllocs& ...__inner_allocs) _NOEXCEPT
447            : base(std::forward<_OuterA2>(__outer_alloc), __inner_allocs...) {}
448    // scoped_allocator_adaptor(const scoped_allocator_adaptor& __other) = default;
449    template <class _OuterA2,
450              __enable_if_t<is_constructible<outer_allocator_type, const _OuterA2&>::value, int> = 0>
451        _LIBCPP_HIDE_FROM_ABI
452        scoped_allocator_adaptor(
453            const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __other) _NOEXCEPT
454                : base(__other) {}
455    template <class _OuterA2,
456              __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
457        _LIBCPP_HIDE_FROM_ABI
458        scoped_allocator_adaptor(
459            scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>&& __other) _NOEXCEPT
460                : base(std::move(__other)) {}
461
462    // scoped_allocator_adaptor& operator=(const scoped_allocator_adaptor&) = default;
463    // scoped_allocator_adaptor& operator=(scoped_allocator_adaptor&&) = default;
464    // ~scoped_allocator_adaptor() = default;
465
466    _LIBCPP_HIDE_FROM_ABI
467    inner_allocator_type& inner_allocator() _NOEXCEPT
468        {return base::inner_allocator();}
469    _LIBCPP_HIDE_FROM_ABI
470    const inner_allocator_type& inner_allocator() const _NOEXCEPT
471        {return base::inner_allocator();}
472
473    _LIBCPP_HIDE_FROM_ABI
474    outer_allocator_type& outer_allocator() _NOEXCEPT
475        {return base::outer_allocator();}
476    _LIBCPP_HIDE_FROM_ABI
477    const outer_allocator_type& outer_allocator() const _NOEXCEPT
478        {return base::outer_allocator();}
479
480    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
481    pointer allocate(size_type __n)
482        {return allocator_traits<outer_allocator_type>::
483            allocate(outer_allocator(), __n);}
484    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
485    pointer allocate(size_type __n, const_void_pointer __hint)
486        {return allocator_traits<outer_allocator_type>::
487            allocate(outer_allocator(), __n, __hint);}
488
489    _LIBCPP_HIDE_FROM_ABI
490    void deallocate(pointer __p, size_type __n) _NOEXCEPT
491        {allocator_traits<outer_allocator_type>::
492            deallocate(outer_allocator(), __p, __n);}
493
494    _LIBCPP_HIDE_FROM_ABI
495    size_type max_size() const
496        {return allocator_traits<outer_allocator_type>::max_size(outer_allocator());}
497
498#if _LIBCPP_STD_VER >= 20
499    template <class _Type, class... _Args>
500    _LIBCPP_HIDE_FROM_ABI void construct(_Type* __ptr, _Args&&... __args) {
501      using _OM = __outermost<outer_allocator_type>;
502      std::apply(
503          [__ptr, this](auto&&... __newargs) {
504            allocator_traits<typename _OM::type>::construct(
505                _OM()(outer_allocator()), __ptr, std::forward<decltype(__newargs)>(__newargs)...);
506          },
507          std::uses_allocator_construction_args<_Type>(inner_allocator(), std::forward<_Args>(__args)...));
508    }
509#else
510    template <class _Tp, class... _Args>
511        _LIBCPP_HIDE_FROM_ABI
512        void construct(_Tp* __p, _Args&& ...__args)
513            {__construct(__uses_alloc_ctor<_Tp, inner_allocator_type&, _Args...>(),
514                         __p, std::forward<_Args>(__args)...);}
515
516    template <class _T1, class _T2, class... _Args1, class... _Args2>
517    _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, piecewise_construct_t,
518                       tuple<_Args1...> __x, tuple<_Args2...> __y)
519    {
520        typedef __outermost<outer_allocator_type> _OM;
521        allocator_traits<typename _OM::type>::construct(
522            _OM()(outer_allocator()), __p, piecewise_construct
523          , __transform_tuple(
524              typename __uses_alloc_ctor<
525                  _T1, inner_allocator_type&, _Args1...
526              >::type()
527            , std::move(__x)
528            , typename __make_tuple_indices<sizeof...(_Args1)>::type{}
529          )
530          , __transform_tuple(
531              typename __uses_alloc_ctor<
532                  _T2, inner_allocator_type&, _Args2...
533              >::type()
534            , std::move(__y)
535            , typename __make_tuple_indices<sizeof...(_Args2)>::type{}
536          )
537        );
538    }
539
540    template <class _T1, class _T2>
541    _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p)
542    { construct(__p, piecewise_construct, tuple<>{}, tuple<>{}); }
543
544    template <class _T1, class _T2, class _Up, class _Vp>
545    _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, _Up&& __x, _Vp&& __y) {
546        construct(__p, piecewise_construct,
547                  std::forward_as_tuple(std::forward<_Up>(__x)),
548                  std::forward_as_tuple(std::forward<_Vp>(__y)));
549    }
550
551    template <class _T1, class _T2, class _Up, class _Vp>
552    _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x) {
553        construct(__p, piecewise_construct,
554                  std::forward_as_tuple(__x.first),
555                  std::forward_as_tuple(__x.second));
556    }
557
558    template <class _T1, class _T2, class _Up, class _Vp>
559    _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x) {
560        construct(__p, piecewise_construct,
561                  std::forward_as_tuple(std::forward<_Up>(__x.first)),
562                  std::forward_as_tuple(std::forward<_Vp>(__x.second)));
563    }
564#endif
565
566    template <class _Tp>
567        _LIBCPP_HIDE_FROM_ABI
568        void destroy(_Tp* __p)
569            {
570                typedef __outermost<outer_allocator_type> _OM;
571                allocator_traits<typename _OM::type>::
572                                         destroy(_OM()(outer_allocator()), __p);
573            }
574
575    _LIBCPP_HIDE_FROM_ABI
576    scoped_allocator_adaptor select_on_container_copy_construction() const _NOEXCEPT
577        {return base::select_on_container_copy_construction();}
578
579private:
580
581
582    template <class _OuterA2,
583              __enable_if_t<is_constructible<outer_allocator_type, _OuterA2>::value, int> = 0>
584    _LIBCPP_HIDE_FROM_ABI
585    scoped_allocator_adaptor(_OuterA2&& __o,
586                             const inner_allocator_type& __i) _NOEXCEPT
587        : base(std::forward<_OuterA2>(__o), __i) {}
588
589    template <class _Tp, class... _Args>
590        _LIBCPP_HIDE_FROM_ABI
591        void __construct(integral_constant<int, 0>, _Tp* __p, _Args&& ...__args)
592            {
593                typedef __outermost<outer_allocator_type> _OM;
594                allocator_traits<typename _OM::type>::construct
595                (
596                    _OM()(outer_allocator()),
597                    __p,
598                    std::forward<_Args>(__args)...
599                );
600            }
601
602    template <class _Tp, class... _Args>
603        _LIBCPP_HIDE_FROM_ABI
604        void __construct(integral_constant<int, 1>, _Tp* __p, _Args&& ...__args)
605            {
606                typedef __outermost<outer_allocator_type> _OM;
607                allocator_traits<typename _OM::type>::construct
608                (
609                    _OM()(outer_allocator()),
610                    __p, allocator_arg, inner_allocator(),
611                    std::forward<_Args>(__args)...
612                );
613            }
614
615    template <class _Tp, class... _Args>
616        _LIBCPP_HIDE_FROM_ABI
617        void __construct(integral_constant<int, 2>, _Tp* __p, _Args&& ...__args)
618            {
619                typedef __outermost<outer_allocator_type> _OM;
620                allocator_traits<typename _OM::type>::construct
621                (
622                    _OM()(outer_allocator()),
623                    __p,
624                    std::forward<_Args>(__args)...,
625                    inner_allocator()
626                );
627            }
628
629    template <class ..._Args, size_t ..._Idx>
630    _LIBCPP_HIDE_FROM_ABI
631    tuple<_Args&&...>
632    __transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t,
633                      __tuple_indices<_Idx...>)
634    {
635        return std::forward_as_tuple(std::get<_Idx>(std::move(__t))...);
636    }
637
638    template <class ..._Args, size_t ..._Idx>
639    _LIBCPP_HIDE_FROM_ABI
640    tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>
641    __transform_tuple(integral_constant<int, 1>, tuple<_Args...> && __t,
642                      __tuple_indices<_Idx...>)
643    {
644        using _Tup = tuple<allocator_arg_t, inner_allocator_type&, _Args&&...>;
645        return _Tup(allocator_arg, inner_allocator(),
646                    std::get<_Idx>(std::move(__t))...);
647    }
648
649    template <class ..._Args, size_t ..._Idx>
650    _LIBCPP_HIDE_FROM_ABI
651    tuple<_Args&&..., inner_allocator_type&>
652    __transform_tuple(integral_constant<int, 2>, tuple<_Args...> && __t,
653                      __tuple_indices<_Idx...>)
654    {
655        using _Tup = tuple<_Args&&..., inner_allocator_type&>;
656        return _Tup(std::get<_Idx>(std::move(__t))..., inner_allocator());
657    }
658
659    template <class...> friend class __scoped_allocator_storage;
660};
661
662#if _LIBCPP_STD_VER >= 17
663template<class _OuterAlloc, class... _InnerAllocs>
664    scoped_allocator_adaptor(_OuterAlloc, _InnerAllocs...)
665        -> scoped_allocator_adaptor<_OuterAlloc, _InnerAllocs...>;
666#endif
667
668template <class _OuterA1, class _OuterA2>
669inline _LIBCPP_HIDE_FROM_ABI
670bool
671operator==(const scoped_allocator_adaptor<_OuterA1>& __a,
672           const scoped_allocator_adaptor<_OuterA2>& __b) _NOEXCEPT
673{
674    return __a.outer_allocator() == __b.outer_allocator();
675}
676
677template <class _OuterA1, class _OuterA2, class _InnerA0, class... _InnerAllocs>
678inline _LIBCPP_HIDE_FROM_ABI
679bool
680operator==(const scoped_allocator_adaptor<_OuterA1, _InnerA0, _InnerAllocs...>& __a,
681           const scoped_allocator_adaptor<_OuterA2, _InnerA0, _InnerAllocs...>& __b) _NOEXCEPT
682{
683    return __a.outer_allocator() == __b.outer_allocator() &&
684           __a.inner_allocator() == __b.inner_allocator();
685}
686
687#if _LIBCPP_STD_VER <= 17
688
689template <class _OuterA1, class _OuterA2, class... _InnerAllocs>
690inline _LIBCPP_HIDE_FROM_ABI
691bool
692operator!=(const scoped_allocator_adaptor<_OuterA1, _InnerAllocs...>& __a,
693           const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __b) _NOEXCEPT
694{
695    return !(__a == __b);
696}
697
698#endif // _LIBCPP_STD_VER <= 17
699
700#endif // !defined(_LIBCPP_CXX03_LANG)
701
702_LIBCPP_END_NAMESPACE_STD
703
704_LIBCPP_POP_MACROS
705
706#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
707#  include <atomic>
708#  include <climits>
709#  include <concepts>
710#  include <cstring>
711#  include <ctime>
712#  include <iterator>
713#  include <memory>
714#  include <ratio>
715#  include <stdexcept>
716#  include <type_traits>
717#  include <variant>
718#endif
719
720#endif // _LIBCPP_SCOPED_ALLOCATOR
721