1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: sanitizer-new-delete
13 // template <class Y, class D> shared_ptr(unique_ptr<Y, D>&&r);
21 #include "test_macros.h"
22 #include "count_new.h"
29 B(const B
&) {++count
;}
30 virtual ~B() {--count
;}
41 A(const A
& other
) : B(other
) {++count
;}
47 void fn ( const std::shared_ptr
<int> &) {}
48 void fn ( const std::shared_ptr
<B
> &) { assert (false); }
51 void assert_deleter ( T
* ) { assert(false); }
55 void operator()(int *) const {}
61 struct StatefulDeleter
{
64 StatefulDeleter(int val
= 0) : state(val
) {}
65 StatefulDeleter(StatefulDeleter
const&) { assert(false); }
67 void operator()(T
* ptr
) {
74 struct StatefulArrayDeleter
{
77 StatefulArrayDeleter(int val
= 0) : state(val
) {}
78 StatefulArrayDeleter(StatefulArrayDeleter
const&) { assert(false); }
80 void operator()(T
* ptr
) {
86 struct MovingDeleter
{
87 explicit MovingDeleter(int *moves
) : moves_(moves
) {}
88 MovingDeleter(MovingDeleter
&& rhs
) : moves_(rhs
.moves_
) { *moves_
+= 1; }
89 void operator()(int*) const {}
93 // https://llvm.org/PR53368
94 // Bogus unique_ptr-to-shared_ptr conversions should be forbidden
95 #if TEST_STD_VER >= 17
96 static_assert( std::is_constructible
<std::shared_ptr
<A
>, std::unique_ptr
<A
>&&>::value
, "");
97 static_assert( std::is_constructible
<std::shared_ptr
<A
[]>, std::unique_ptr
<A
[]>&&>::value
, "");
98 static_assert(!std::is_constructible
<std::shared_ptr
<A
>, std::unique_ptr
<A
[]>&&>::value
, "");
99 static_assert(!std::is_constructible
<std::shared_ptr
<B
[]>, std::unique_ptr
<A
[]>&&>::value
, "");
100 static_assert(!std::is_constructible
<std::shared_ptr
<B
>, std::unique_ptr
<A
[]>&&>::value
, "");
103 int main(int, char**)
106 std::unique_ptr
<A
> ptr(new A
);
107 A
* raw_ptr
= ptr
.get();
108 std::shared_ptr
<B
> p(std::move(ptr
));
109 assert(A::count
== 1);
110 assert(B::count
== 1);
111 assert(p
.use_count() == 1);
112 assert(p
.get() == raw_ptr
);
113 assert(ptr
.get() == 0);
117 std::unique_ptr
<A
const> ptr(new A
);
118 A
const* raw_ptr
= ptr
.get();
119 std::shared_ptr
<B
const> p(std::move(ptr
));
120 assert(A::count
== 1);
121 assert(B::count
== 1);
122 assert(p
.use_count() == 1);
123 assert(p
.get() == raw_ptr
);
124 assert(ptr
.get() == 0);
127 #ifndef TEST_HAS_NO_EXCEPTIONS
128 assert(A::count
== 0);
130 std::unique_ptr
<A
> ptr(new A
);
131 A
* raw_ptr
= ptr
.get();
132 globalMemCounter
.throw_after
= 0;
135 std::shared_ptr
<B
> p(std::move(ptr
));
140 assert(A::count
== 1);
141 assert(B::count
== 1);
142 assert(ptr
.get() == raw_ptr
);
147 #if TEST_STD_VER > 14
149 std::unique_ptr
<int> ptr
;
150 std::shared_ptr
<int> p(std::move(ptr
));
151 assert(p
.get() == 0);
152 assert(p
.use_count() == 0);
157 StatefulDeleter
<A
> d
;
158 std::unique_ptr
<A
, StatefulDeleter
<A
>&> u(new A
, d
);
159 std::shared_ptr
<A
> p(std::move(u
));
161 assert(A::count
== 1);
163 assert(A::count
== 0);
166 fn(std::unique_ptr
<int>(new int));
168 #if TEST_STD_VER >= 14
170 std::unique_ptr
<int, void (*)(int*)> p(nullptr, assert_deleter
<int>);
171 std::shared_ptr
<int> p2(std::move(p
)); // should not call deleter when going out of scope
177 std::unique_ptr
<int, adl::D
&> u(nullptr, d
);
178 std::shared_ptr
<int> s
= std::move(u
);
181 assert(A::count
== 0);
183 #if TEST_STD_VER > 14
185 StatefulArrayDeleter
<A
> d
;
186 std::unique_ptr
<A
[], StatefulArrayDeleter
<A
>&> u(new A
[4], d
);
187 std::shared_ptr
<A
[]> p(std::move(u
));
189 assert(A::count
== 4);
191 assert(A::count
== 0);
192 assert(B::count
== 0);
195 std::unique_ptr
<A
[]> ptr(new A
[8]);
196 A
* raw_ptr
= ptr
.get();
197 std::shared_ptr
<A
[]> p(std::move(ptr
));
198 assert(A::count
== 8);
199 assert(p
.use_count() == 1);
200 assert(p
.get() == raw_ptr
);
201 assert(ptr
.get() == 0);
203 assert(A::count
== 0);
207 std::unique_ptr
<int[]> u(p
);
208 std::shared_ptr
<int[]> s(std::move(u
));
209 assert(u
== nullptr);
210 assert(s
.get() == p
);
212 #endif // TEST_STD_VER > 14
218 std::unique_ptr
<int, MovingDeleter
> u(&i
, MovingDeleter(&moves
));
220 std::shared_ptr
<int> s(std::move(u
));
222 assert(u
== nullptr);
223 assert(s
.get() == &i
);
226 #if TEST_STD_VER > 14
230 std::unique_ptr
<int[], MovingDeleter
> u(a
, MovingDeleter(&moves
));
232 std::shared_ptr
<int[]> s
= std::move(u
);
234 assert(u
== nullptr);
235 assert(s
.get() == a
);
237 #endif // TEST_STD_VER > 14