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 D> shared_ptr(nullptr_t, 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 struct A
22 {
23     static int count;
24 
AA25     A() {++count;}
AA26     A(const A&) {++count;}
~AA27     ~A() {--count;}
28 };
29 
30 int A::count = 0;
31 
32 // LWG 3233. Broken requirements for shared_ptr converting constructors
33 // https://cplusplus.github.io/LWG/issue3233
34 static_assert( std::is_constructible<std::shared_ptr<int>,  std::nullptr_t, test_deleter<int> >::value, "");
35 static_assert(!std::is_constructible<std::shared_ptr<int>,  std::nullptr_t, bad_deleter>::value, "");
36 static_assert(!std::is_constructible<std::shared_ptr<int>,  std::nullptr_t, no_nullptr_deleter>::value, "");
37 static_assert(!std::is_constructible<std::shared_ptr<int>,  std::nullptr_t, no_move_deleter>::value, "");
38 
39 #if TEST_STD_VER >= 17
40 static_assert( std::is_constructible<std::shared_ptr<int[]>,  std::nullptr_t, test_deleter<int> >::value, "");
41 static_assert(!std::is_constructible<std::shared_ptr<int[]>,  std::nullptr_t, bad_deleter>::value, "");
42 static_assert(!std::is_constructible<std::shared_ptr<int[]>,  std::nullptr_t, no_nullptr_deleter>::value, "");
43 static_assert(!std::is_constructible<std::shared_ptr<int[]>,  std::nullptr_t, no_move_deleter>::value, "");
44 
45 static_assert( std::is_constructible<std::shared_ptr<int[5]>,  std::nullptr_t, test_deleter<int> >::value, "");
46 static_assert(!std::is_constructible<std::shared_ptr<int[5]>,  std::nullptr_t, bad_deleter>::value, "");
47 static_assert(!std::is_constructible<std::shared_ptr<int[5]>,  std::nullptr_t, no_nullptr_deleter>::value, "");
48 static_assert(!std::is_constructible<std::shared_ptr<int[5]>,  std::nullptr_t, no_move_deleter>::value, "");
49 #endif
50 
main(int,char **)51 int main(int, char**)
52 {
53     {
54         std::shared_ptr<A> p(nullptr, test_deleter<A>(3));
55         assert(A::count == 0);
56         assert(p.use_count() == 1);
57         assert(p.get() == 0);
58         assert(test_deleter<A>::count == 1);
59         assert(test_deleter<A>::dealloc_count == 0);
60 #ifndef TEST_HAS_NO_RTTI
61         test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p);
62         assert(d);
63         assert(d->state() == 3);
64 #endif
65     }
66     assert(A::count == 0);
67     assert(test_deleter<A>::count == 0);
68     assert(test_deleter<A>::dealloc_count == 1);
69 
70     {
71         std::shared_ptr<A const> p(nullptr, test_deleter<A const>(3));
72         assert(p.get() == nullptr);
73     }
74 
75     return 0;
76 }
77