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
20struct A
21{
22 static int count;
23
24 A() {++count;}
25 A(const A&) {++count;}
26 ~A() {--count;}
27};
28
29int A::count = 0;
30
31struct bad_ty { };
32
33struct bad_deleter
34{
35 void operator()(bad_ty) { }
36};
37
38struct no_move_deleter
39{
40 no_move_deleter(no_move_deleter const&) = delete;
41 no_move_deleter(no_move_deleter &&) = delete;
42 void operator()(int*) { }
43};
44
45static_assert(!std::is_move_constructible<no_move_deleter>::value, "");
46
47struct Base { };
48struct Derived : Base { };
49
50template<class T>
51class MoveDeleter
52{
53 MoveDeleter();
54 MoveDeleter(MoveDeleter const&);
55public:
56 MoveDeleter(MoveDeleter&&) {}
57
58 explicit MoveDeleter(int) {}
59
60 void operator()(T* ptr) { delete ptr; }
61};
62
63// https://llvm.org/PR60258
64// Invalid constructor SFINAE for std::shared_ptr's array ctors
65static_assert( std::is_constructible<std::shared_ptr<int>, int*, test_deleter<int> >::value, "");
66static_assert(!std::is_constructible<std::shared_ptr<int>, int*, bad_deleter>::value, "");
67static_assert( std::is_constructible<std::shared_ptr<Base>, Derived*, test_deleter<Base> >::value, "");
68static_assert(!std::is_constructible<std::shared_ptr<A>, int*, test_deleter<A> >::value, "");
69
70#if TEST_STD_VER >= 17
71static_assert( std::is_constructible<std::shared_ptr<int[]>, int*, test_deleter<int>>::value, "");
72static_assert(!std::is_constructible<std::shared_ptr<int[]>, int*, bad_deleter>::value, "");
73static_assert(!std::is_constructible<std::shared_ptr<int[]>, int(*)[], test_deleter<int>>::value, "");
74static_assert( std::is_constructible<std::shared_ptr<int[5]>, int*, test_deleter<int>>::value, "");
75static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int*, bad_deleter>::value, "");
76static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int(*)[5], test_deleter<int>>::value, "");
77#endif
78
79int main(int, char**)
80{
81 {
82 A* ptr = new A;
83 std::shared_ptr<A> p(ptr, test_deleter<A>(3));
84 assert(A::count == 1);
85 assert(p.use_count() == 1);
86 assert(p.get() == ptr);
87 assert(test_deleter<A>::count == 1);
88 assert(test_deleter<A>::dealloc_count == 0);
89#ifndef TEST_HAS_NO_RTTI
90 test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p);
91 assert(d);
92 assert(d->state() == 3);
93#endif
94 }
95 assert(A::count == 0);
96 assert(test_deleter<A>::count == 0);
97 assert(test_deleter<A>::dealloc_count == 1);
98
99 {
100 A const* ptr = new A;
101 std::shared_ptr<A const> p(ptr, test_deleter<A const>(3));
102 assert(p.get() == ptr);
103 }
104
105 {
106 // Make sure we can't construct with:
107 // a) a deleter that doesn't have an operator ()(int*)
108 // b) a deleter that doesn't have a move constructor.
109 static_assert(!std::is_constructible<std::shared_ptr<int>, int*, bad_deleter>::value, "");
110 static_assert(!std::is_constructible<std::shared_ptr<int>, int*, no_move_deleter>::value, "");
111
112 // Make sure that we can construct a shared_ptr where the element type and pointer type
113 // aren't "convertible" but are "compatible".
114 static_assert(!std::is_constructible<std::shared_ptr<Derived[4]>, Base[4], test_deleter<Derived[4]> >::value, "");
115 }
116
117#if TEST_STD_VER >= 11
118 {
119 MoveDeleter<int> d(0);
120 std::shared_ptr<int> p0(new int, std::move(d));
121 std::shared_ptr<int> p1(nullptr, std::move(d));
122 }
123#endif // TEST_STD_VER >= 11
124
125 return 0;
126}
127

source code of libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter.pass.cpp