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