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 #include "types.h"
21 
22 struct A
23 {
24     static int count;
25 
AA26     A() {++count;}
AA27     A(const A&) {++count;}
~AA28     ~A() {--count;}
29 };
30 
31 int A::count = 0;
32 
33 // LWG 3233. Broken requirements for shared_ptr converting constructors
34 // https://cplusplus.github.io/LWG/issue3233
35 // https://llvm.org/PR60258
36 // Invalid constructor SFINAE for std::shared_ptr's array ctors
37 static_assert( std::is_constructible<std::shared_ptr<int>,  int*, test_deleter<int> >::value, "");
38 static_assert(!std::is_constructible<std::shared_ptr<int>,  int*, bad_deleter>::value, "");
39 static_assert( std::is_constructible<std::shared_ptr<base>,  derived*, test_deleter<base> >::value, "");
40 static_assert(!std::is_constructible<std::shared_ptr<A>,  int*, test_deleter<A> >::value, "");
41 
42 #if TEST_STD_VER >= 17
43 static_assert( std::is_constructible<std::shared_ptr<int[]>,  int*, test_deleter<int> >::value, "");
44 static_assert(!std::is_constructible<std::shared_ptr<int[]>,  int*, bad_deleter>::value, "");
45 static_assert(!std::is_constructible<std::shared_ptr<int[]>,  int(*)[], test_deleter<int> >::value, "");
46 static_assert( std::is_constructible<std::shared_ptr<int[5]>, int*, test_deleter<int> >::value, "");
47 static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int*, bad_deleter>::value, "");
48 static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int(*)[5], test_deleter<int> >::value, "");
49 #endif
50 
f()51 int f() { return 5; }
52 
53 // https://cplusplus.github.io/LWG/issue3018
54 // LWG 3018. shared_ptr of function type
55 struct function_pointer_deleter {
function_pointer_deleterfunction_pointer_deleter56   function_pointer_deleter(bool& deleter_called) : deleter_called_(deleter_called) {}
57 
operator ()function_pointer_deleter58   void operator()(int (*)()) const { deleter_called_ = true; }
59 
60   bool& deleter_called_;
61 };
62 
test_function_type()63 void test_function_type() {
64   bool deleter_called = false;
65   {
66     std::shared_ptr<int()> p(&f, function_pointer_deleter(deleter_called));
67     assert((*p)() == 5);
68   }
69   assert(deleter_called);
70 }
71 
main(int,char **)72 int main(int, char**)
73 {
74     {
75         A* ptr = new A;
76         std::shared_ptr<A> p(ptr, test_deleter<A>(3));
77         assert(A::count == 1);
78         assert(p.use_count() == 1);
79         assert(p.get() == ptr);
80         assert(test_deleter<A>::count == 1);
81         assert(test_deleter<A>::dealloc_count == 0);
82 #ifndef TEST_HAS_NO_RTTI
83         test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p);
84         assert(d);
85         assert(d->state() == 3);
86 #endif
87     }
88     assert(A::count == 0);
89     assert(test_deleter<A>::count == 0);
90     assert(test_deleter<A>::dealloc_count == 1);
91 
92     {
93         A const* ptr = new A;
94         std::shared_ptr<A const> p(ptr, test_deleter<A const>(3));
95         assert(p.get() == ptr);
96     }
97 
98     {
99         // Make sure we can't construct with:
100         //    a) a deleter that doesn't have an operator ()(int*)
101         //    b) a deleter that doesn't have a move constructor.
102         static_assert(!std::is_constructible<std::shared_ptr<int>, int*, bad_deleter>::value, "");
103         static_assert(!std::is_constructible<std::shared_ptr<int>, int*, no_move_deleter>::value, "");
104 
105         // Make sure that we can construct a shared_ptr where the element type and pointer type
106         // aren't "convertible" but are "compatible".
107         static_assert(!std::is_constructible<std::shared_ptr<derived[4]>, base[4], test_deleter<derived[4]> >::value, "");
108     }
109 
110 #if TEST_STD_VER >= 11
111     {
112         move_deleter<int> d(0);
113         std::shared_ptr<int> p0(new int, std::move(d));
114         std::shared_ptr<int> p1(nullptr, std::move(d));
115     }
116 #endif // TEST_STD_VER >= 11
117 
118 #if TEST_STD_VER >= 14
119     {
120       // LWG 4110
121       auto deleter = [](auto pointer) { delete pointer; };
122       std::shared_ptr<int> p(new int, deleter);
123     }
124 
125     {
126       std::shared_ptr<int> p(NULL, [](auto){});
127     }
128 #endif
129 
130 #if TEST_STD_VER >= 17
131     {
132       // See https://github.com/llvm/llvm-project/pull/93071#issuecomment-2166047398
133       std::shared_ptr<char[]> a(new char[10], std::default_delete<char[]>());
134     }
135 #endif
136 
137   test_function_type();
138   return 0;
139 }
140