1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <scoped_allocator>
12 
13 // template <class OuterAlloc, class... InnerAllocs>
14 //   class scoped_allocator_adaptor
15 
16 // template <class OuterA2>
17 //   scoped_allocator_adaptor(OuterA2&& outerAlloc,
18 //                            const InnerAllocs& ...innerAllocs);
19 
20 #include <scoped_allocator>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "allocators.h"
25 
main(int,char **)26 int main(int, char**)
27 {
28     {
29         typedef std::scoped_allocator_adaptor<A1<int>> A;
30         A1<int> a3(3);
31         A a(a3);
32         assert(a.outer_allocator() == A1<int>(3));
33         assert(a.inner_allocator() == a);
34         assert(A1<int>::copy_called == true);
35         assert(A1<int>::move_called == false);
36     }
37     A1<int>::copy_called = false;
38     {
39         typedef std::scoped_allocator_adaptor<A1<int>> A;
40         A a(A1<int>(3));
41         assert(a.outer_allocator() == A1<int>(3));
42         assert(a.inner_allocator() == a);
43         assert(A1<int>::copy_called == false);
44         assert(A1<int>::move_called == true);
45     }
46     A1<int>::move_called = false;
47     {
48         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>> A;
49         A1<int> a4(4);
50         A a(a4, A2<int>(5));
51         assert(A1<int>::copy_called == true);
52         assert(A1<int>::move_called == false);
53         assert(A2<int>::copy_called == true);
54         assert(A2<int>::move_called == false);
55         assert(a.outer_allocator() == A1<int>(4));
56         assert(a.inner_allocator() == std::scoped_allocator_adaptor<A2<int>>(A2<int>(5)));
57     }
58     A1<int>::copy_called = false;
59     A1<int>::move_called = false;
60     A2<int>::copy_called = false;
61     A2<int>::move_called = false;
62     {
63         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>> A;
64         A a(A1<int>(4), A2<int>(5));
65         assert(A1<int>::copy_called == false);
66         assert(A1<int>::move_called == true);
67         assert(A2<int>::copy_called == true);
68         assert(A2<int>::move_called == false);
69         assert(a.outer_allocator() == A1<int>(4));
70         assert(a.inner_allocator() == std::scoped_allocator_adaptor<A2<int>>(A2<int>(5)));
71     }
72     A1<int>::copy_called = false;
73     A1<int>::move_called = false;
74     A2<int>::copy_called = false;
75     A2<int>::move_called = false;
76     A1<int>::move_called = false;
77     {
78         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>, A3<int>> A;
79         A1<int> a4(4);
80         A a(a4, A2<int>(5), A3<int>(6));
81         assert(A1<int>::copy_called == true);
82         assert(A1<int>::move_called == false);
83         assert(A2<int>::copy_called == true);
84         assert(A2<int>::move_called == false);
85         assert(A3<int>::copy_called == true);
86         assert(A3<int>::move_called == false);
87         assert(a.outer_allocator() == A1<int>(4));
88         assert((a.inner_allocator() ==
89             std::scoped_allocator_adaptor<A2<int>, A3<int>>(A2<int>(5), A3<int>(6))));
90     }
91     A1<int>::copy_called = false;
92     A1<int>::move_called = false;
93     A2<int>::copy_called = false;
94     A2<int>::move_called = false;
95     A3<int>::copy_called = false;
96     A3<int>::move_called = false;
97     {
98         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>, A3<int>> A;
99         A a(A1<int>(4), A2<int>(5), A3<int>(6));
100         assert(A1<int>::copy_called == false);
101         assert(A1<int>::move_called == true);
102         assert(A2<int>::copy_called == true);
103         assert(A2<int>::move_called == false);
104         assert(A3<int>::copy_called == true);
105         assert(A3<int>::move_called == false);
106         assert(a.outer_allocator() == A1<int>(4));
107         assert((a.inner_allocator() ==
108             std::scoped_allocator_adaptor<A2<int>, A3<int>>(A2<int>(5), A3<int>(6))));
109     }
110 //  Test for LWG2782
111     {
112         static_assert(!std::is_convertible<A1<int>, A2<int>>::value, "");
113         static_assert(!std::is_convertible<
114              std::scoped_allocator_adaptor<A1<int>>,
115              std::scoped_allocator_adaptor<A2<int>>>::value, "");
116     }
117 
118   return 0;
119 }
120