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 // <memory>
10
11 // shared_ptr
12
13 // template<class Y, class D> shared_ptr(Y* p, D d);
14
15 #include <memory>
16 #include <cassert>
17 #include "test_macros.h"
18 #include "deleter_types.h"
19
20 struct A
21 {
22 static int count;
23
AA24 A() {++count;}
AA25 A(const A&) {++count;}
~AA26 ~A() {--count;}
27 };
28
29 int A::count = 0;
30
31 struct bad_ty { };
32
33 struct bad_deleter
34 {
operator ()bad_deleter35 void operator()(bad_ty) { }
36 };
37
38 struct no_move_deleter
39 {
40 no_move_deleter(no_move_deleter const&) = delete;
41 no_move_deleter(no_move_deleter &&) = delete;
operator ()no_move_deleter42 void operator()(int*) { }
43 };
44
45 static_assert(!std::is_move_constructible<no_move_deleter>::value, "");
46
47 struct Base { };
48 struct Derived : Base { };
49
50 template<class T>
51 class MoveDeleter
52 {
53 MoveDeleter();
54 MoveDeleter(MoveDeleter const&);
55 public:
MoveDeleter(MoveDeleter &&)56 MoveDeleter(MoveDeleter&&) {};
57
MoveDeleter(int)58 explicit MoveDeleter(int) {}
59
operator ()(T * ptr)60 void operator()(T *ptr) { delete ptr; }
61 };
62
main(int,char **)63 int main(int, char**)
64 {
65 {
66 A* ptr = new A;
67 std::shared_ptr<A> p(ptr, test_deleter<A>(3));
68 assert(A::count == 1);
69 assert(p.use_count() == 1);
70 assert(p.get() == ptr);
71 assert(test_deleter<A>::count == 1);
72 assert(test_deleter<A>::dealloc_count == 0);
73 #ifndef TEST_HAS_NO_RTTI
74 test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p);
75 assert(d);
76 assert(d->state() == 3);
77 #endif
78 }
79 assert(A::count == 0);
80 assert(test_deleter<A>::count == 0);
81 assert(test_deleter<A>::dealloc_count == 1);
82
83 {
84 A const* ptr = new A;
85 std::shared_ptr<A const> p(ptr, test_deleter<A const>(3));
86 assert(p.get() == ptr);
87 }
88
89 {
90 // Make sure we can't construct with:
91 // a) a deleter that doesn't have an operator ()(int*)
92 // b) a deleter that doesn't have a move constructor.
93 static_assert(!std::is_constructible<std::shared_ptr<int>, int*, bad_deleter>::value, "");
94 static_assert(!std::is_constructible<std::shared_ptr<int>, int*, no_move_deleter>::value, "");
95
96 // Make sure that we can construct a shared_ptr where the element type and pointer type
97 // aren't "convertible" but are "compatible".
98 static_assert(!std::is_constructible<std::shared_ptr<Derived[4]>, Base[4], test_deleter<Derived[4]> >::value, "");
99 }
100
101 #if TEST_STD_VER >= 11
102 {
103 MoveDeleter<int> d(0);
104 std::shared_ptr<int> p0(new int, std::move(d));
105 std::shared_ptr<int> p1(nullptr, std::move(d));
106 }
107 #endif // TEST_STD_VER >= 11
108
109 return 0;
110 }
111