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"
21struct A
22{
23 static int count;
24
25 A() {++count;}
26 A(const A&) {++count;}
27 ~A() {--count;}
28};
29
30int A::count = 0;
31
32// LWG 3233. Broken requirements for shared_ptr converting constructors
33// https://cplusplus.github.io/LWG/issue3233
34static_assert( std::is_constructible<std::shared_ptr<int>, std::nullptr_t, test_deleter<int> >::value, "");
35static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, bad_deleter>::value, "");
36static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, no_nullptr_deleter>::value, "");
37static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, no_move_deleter>::value, "");
38
39#if TEST_STD_VER >= 17
40static_assert( std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, test_deleter<int> >::value, "");
41static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, bad_deleter>::value, "");
42static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, no_nullptr_deleter>::value, "");
43static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, no_move_deleter>::value, "");
44
45static_assert( std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, test_deleter<int> >::value, "");
46static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, bad_deleter>::value, "");
47static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, no_nullptr_deleter>::value, "");
48static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, no_move_deleter>::value, "");
49#endif
50
51int 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

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