]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/array.cc
1 // { dg-do run { target c++11 } }
3 // Copyright (C) 2016-2021 Free Software Foundation, Inc.
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)
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.
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/>.
21 #include <testsuite_hooks.h>
23 // C++17 20.11.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
25 template<typename To
, typename From
>
26 constexpr bool check()
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
;
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>() );
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
[]>() );
63 std::shared_ptr
<A
[2]> p
;
64 VERIFY( p
.get() == nullptr );
65 VERIFY( p
.use_count() == 0 );
73 std::shared_ptr
<A
[]> p
;
74 VERIFY( p
.get() == nullptr );
75 VERIFY( p
.use_count() == 0 );
83 std::shared_ptr
<A
[2]> p(nullptr);
84 VERIFY( p
.get() == nullptr );
85 VERIFY( p
.use_count() == 0 );
93 std::shared_ptr
<A
[]> p(nullptr);
94 VERIFY( p
.get() == nullptr );
95 VERIFY( p
.use_count() == 0 );
100 // Construction from pointer
105 A
* const a
= nullptr;
106 std::shared_ptr
<A
[2]> p(a
);
107 VERIFY( p
.get() == nullptr );
108 VERIFY( p
.use_count() == 1 );
110 VERIFY( count
== 0 );
116 A
* const a
= nullptr;
117 std::shared_ptr
<A
[]> p(a
);
118 VERIFY( p
.get() == nullptr );
119 VERIFY( p
.use_count() == 1 );
121 VERIFY( count
== 0 );
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 );
132 VERIFY( count
== 0 );
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 );
143 VERIFY( count
== 0 );
146 // Converting constrcutor
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 );
158 VERIFY( count
!= 0 );
160 VERIFY( count
== 0 );
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 );
173 VERIFY( count
!= 0 );
175 VERIFY( count
== 0 );
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 );
188 VERIFY( count
!= 0 );
190 VERIFY( count
== 0 );
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 );
205 VERIFY( count
!= 0 );
207 VERIFY( count
== 0 );
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 );
220 VERIFY( count
!= 0 );
222 VERIFY( count
== 0 );
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 );
238 VERIFY( count
== 0 );
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 );
252 VERIFY( count
== 0 );