]>
Commit | Line | Data |
---|---|---|
b2343559 JW |
1 | // { dg-do run { target c++11 } } |
2 | ||
85ec4feb | 3 | // Copyright (C) 2016-2018 Free Software Foundation, Inc. |
b2343559 JW |
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 | // You should have received a copy of the GNU General Public License along | |
17 | // with this library; see the file COPYING3. If not see | |
18 | // <http://www.gnu.org/licenses/>. | |
19 | ||
20 | #include <memory> | |
21 | #include <testsuite_hooks.h> | |
22 | ||
7b936140 | 23 | // C++17 20.11.2.2.1 shared_ptr constructors [util.smartptr.shared.const] |
b2343559 JW |
24 | |
25 | template<typename To, typename From> | |
26 | constexpr bool check() | |
27 | { | |
28 | using std::shared_ptr; | |
29 | using std::is_constructible; | |
30 | return !is_constructible<shared_ptr<To>, shared_ptr<From>>::value | |
31 | && !is_constructible<shared_ptr<To>, shared_ptr<From>&>::value; | |
32 | } | |
33 | ||
34 | static_assert( check<int, int[]>() ); | |
35 | static_assert( check<int, int[2]>() ); | |
36 | static_assert( check<int[2], void>() ); | |
37 | static_assert( check<int[2], int>() ); | |
38 | static_assert( check<int[2], int[]>() ); | |
39 | static_assert( check<int[], void>() ); | |
40 | static_assert( check<int[], int>() ); | |
41 | ||
42 | int count = 0; | |
43 | ||
44 | struct A { | |
45 | A() { ++count; } | |
46 | ~A() { --count; } | |
47 | }; | |
48 | ||
49 | struct B : A { }; | |
50 | ||
51 | static_assert( check<A, B[2]>() ); | |
52 | static_assert( check<A, B[]>() ); | |
53 | static_assert( check<A[2], B>() ); | |
54 | static_assert( check<A[2], B[2]>() ); | |
55 | static_assert( check<A[2], B[]>() ); | |
56 | static_assert( check<A[], B>() ); | |
57 | static_assert( check<A[], B[2]>() ); | |
58 | static_assert( check<A[], B[]>() ); | |
59 | ||
60 | void | |
61 | test01() | |
62 | { | |
63 | std::shared_ptr<A[2]> p; | |
64 | VERIFY( p.get() == nullptr ); | |
65 | VERIFY( p.use_count() == 0 ); | |
66 | p.reset(); | |
67 | VERIFY( count == 0 ); | |
68 | } | |
69 | ||
70 | void | |
71 | test02() | |
72 | { | |
73 | std::shared_ptr<A[]> p; | |
74 | VERIFY( p.get() == nullptr ); | |
75 | VERIFY( p.use_count() == 0 ); | |
76 | p.reset(); | |
77 | VERIFY( count == 0 ); | |
78 | } | |
79 | ||
80 | void | |
81 | test03() | |
82 | { | |
83 | std::shared_ptr<A[2]> p(nullptr); | |
84 | VERIFY( p.get() == nullptr ); | |
85 | VERIFY( p.use_count() == 0 ); | |
86 | p.reset(); | |
87 | VERIFY( count == 0 ); | |
88 | } | |
89 | ||
90 | void | |
91 | test04() | |
92 | { | |
93 | std::shared_ptr<A[]> p(nullptr); | |
94 | VERIFY( p.get() == nullptr ); | |
95 | VERIFY( p.use_count() == 0 ); | |
96 | p.reset(); | |
97 | VERIFY( count == 0 ); | |
98 | } | |
99 | ||
100 | // Construction from pointer | |
101 | ||
102 | void | |
103 | test05() | |
104 | { | |
105 | A * const a = nullptr; | |
106 | std::shared_ptr<A[2]> p(a); | |
107 | VERIFY( p.get() == nullptr ); | |
108 | VERIFY( p.use_count() == 1 ); | |
109 | p.reset(); | |
110 | VERIFY( count == 0 ); | |
111 | } | |
112 | ||
113 | void | |
114 | test06() | |
115 | { | |
116 | A * const a = nullptr; | |
117 | std::shared_ptr<A[]> p(a); | |
118 | VERIFY( p.get() == nullptr ); | |
119 | VERIFY( p.use_count() == 1 ); | |
120 | p.reset(); | |
121 | VERIFY( count == 0 ); | |
122 | } | |
123 | ||
124 | void | |
125 | test07() | |
126 | { | |
127 | A * const a = new A[2]; | |
128 | std::shared_ptr<A[2]> p(a); | |
129 | VERIFY( p.get() == a ); | |
130 | VERIFY( p.use_count() == 1 ); | |
131 | p.reset(); | |
132 | VERIFY( count == 0 ); | |
133 | } | |
134 | ||
135 | void | |
136 | test08() | |
137 | { | |
138 | A * const a = new A[2]; | |
139 | std::shared_ptr<A[]> p(a); | |
140 | VERIFY( p.get() == a ); | |
141 | VERIFY( p.use_count() == 1 ); | |
142 | p.reset(); | |
143 | VERIFY( count == 0 ); | |
144 | } | |
145 | ||
146 | // Converting constrcutor | |
147 | ||
148 | void | |
149 | test09() | |
150 | { | |
151 | A * const a = new A[2]; | |
152 | std::shared_ptr<A[2]> p(a); | |
153 | std::shared_ptr<const A[2]> p2(p); | |
154 | VERIFY( p2.get() == a ); | |
155 | VERIFY( p.use_count() == 2 ); | |
156 | VERIFY( p2.use_count() == 2 ); | |
157 | p.reset(); | |
158 | VERIFY( count != 0 ); | |
159 | p2.reset(); | |
160 | VERIFY( count == 0 ); | |
161 | } | |
162 | ||
163 | void | |
164 | test10() | |
165 | { | |
166 | A * const a = new A[2]; | |
167 | std::shared_ptr<A[]> p(a); | |
168 | std::shared_ptr<const A[]> p2(p); | |
169 | VERIFY( p2.get() == a ); | |
170 | VERIFY( p.use_count() == 2 ); | |
171 | VERIFY( p2.use_count() == 2 ); | |
172 | p.reset(); | |
173 | VERIFY( count != 0 ); | |
174 | p2.reset(); | |
175 | VERIFY( count == 0 ); | |
176 | } | |
177 | ||
178 | void | |
179 | test11() | |
180 | { | |
181 | A * const a = new A[2]; | |
182 | std::shared_ptr<A[2]> p(a); | |
183 | std::shared_ptr<const A[]> p2(p); | |
184 | VERIFY( p2.get() == a ); | |
185 | VERIFY( p.use_count() == 2 ); | |
186 | VERIFY( p2.use_count() == 2 ); | |
187 | p.reset(); | |
188 | VERIFY( count != 0 ); | |
189 | p2.reset(); | |
190 | VERIFY( count == 0 ); | |
191 | } | |
192 | ||
193 | // Copy construction | |
194 | ||
195 | void | |
196 | test12() | |
197 | { | |
198 | A * const a = new A[2]; | |
199 | std::shared_ptr<A[2]> p(a); | |
200 | std::shared_ptr<A[2]> p2(p); | |
201 | VERIFY( p2.get() == a ); | |
202 | VERIFY( p.use_count() == 2 ); | |
203 | VERIFY( p2.use_count() == 2 ); | |
204 | p.reset(); | |
205 | VERIFY( count != 0 ); | |
206 | p2.reset(); | |
207 | VERIFY( count == 0 ); | |
208 | } | |
209 | ||
210 | void | |
211 | test13() | |
212 | { | |
213 | A * const a = new A[2]; | |
214 | std::shared_ptr<A[2]> p(a); | |
215 | std::shared_ptr<A[]> p2(p); | |
216 | VERIFY( p2.get() == a ); | |
217 | VERIFY( p.use_count() == 2 ); | |
218 | VERIFY( p2.use_count() == 2 ); | |
219 | p.reset(); | |
220 | VERIFY( count != 0 ); | |
221 | p2.reset(); | |
222 | VERIFY( count == 0 ); | |
223 | } | |
224 | ||
225 | // Move construction | |
226 | ||
227 | void | |
228 | test14() | |
229 | { | |
230 | A * const a = new A[2]; | |
231 | std::shared_ptr<A[2]> p(a); | |
232 | std::shared_ptr<A[2]> p2(std::move(p)); | |
233 | VERIFY( p.get() == nullptr ); | |
234 | VERIFY( p2.get() == a ); | |
235 | VERIFY( p.use_count() == 0 ); | |
236 | VERIFY( p2.use_count() == 1 ); | |
237 | p2.reset(); | |
238 | VERIFY( count == 0 ); | |
239 | } | |
240 | ||
241 | void | |
242 | test15() | |
243 | { | |
244 | A * const a = new A[2]; | |
245 | std::shared_ptr<A[2]> p(a); | |
246 | std::shared_ptr<A[]> p2(std::move(p)); | |
247 | VERIFY( p.get() == nullptr ); | |
248 | VERIFY( p2.get() == a ); | |
249 | VERIFY( p.use_count() == 0 ); | |
250 | VERIFY( p2.use_count() == 1 ); | |
251 | p2.reset(); | |
252 | VERIFY( count == 0 ); | |
253 | } | |
254 | ||
255 | int | |
256 | main() | |
257 | { | |
258 | test01(); | |
259 | test02(); | |
260 | test03(); | |
261 | test04(); | |
262 | test05(); | |
263 | test06(); | |
264 | test07(); | |
265 | test08(); | |
266 | test09(); | |
267 | test10(); | |
268 | test11(); | |
269 | test12(); | |
270 | test13(); | |
271 | test14(); | |
272 | test15(); | |
273 | } |