| 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> void reset(Y* p); |
| 14 |
|
| 15 |
#include <memory> |
| 16 |
#include <cassert> |
| 17 |
|
| 18 |
#include "reset_helper.h" |
| 19 |
#include "test_macros.h" |
| 20 |
|
| 21 |
struct B |
| 22 |
{ |
| 23 |
static int count; |
| 24 |
|
| 25 |
B() {++count;} |
| 26 |
B(const B&) {++count;} |
| 27 |
virtual ~B() {--count;} |
| 28 |
}; |
| 29 |
|
| 30 |
int B::count = 0; |
| 31 |
|
| 32 |
struct A |
| 33 |
: public B |
| 34 |
{ |
| 35 |
static int count; |
| 36 |
|
| 37 |
A() {++count;} |
| 38 |
A(const A& other) : B(other) {++count;} |
| 39 |
~A() {--count;} |
| 40 |
}; |
| 41 |
|
| 42 |
int A::count = 0; |
| 43 |
|
| 44 |
struct Derived : A {}; |
| 45 |
|
| 46 |
static_assert( HasReset<std::shared_ptr<int>, int*>::value, ""); |
| 47 |
static_assert( HasReset<std::shared_ptr<A>, Derived*>::value, ""); |
| 48 |
static_assert(!HasReset<std::shared_ptr<A>, int*>::value, ""); |
| 49 |
|
| 50 |
#if TEST_STD_VER >= 17 |
| 51 |
static_assert( HasReset<std::shared_ptr<int[]>, int*>::value, ""); |
| 52 |
static_assert(!HasReset<std::shared_ptr<int[]>, int(*)[]>::value, ""); |
| 53 |
static_assert( HasReset<std::shared_ptr<int[5]>, int*>::value, ""); |
| 54 |
static_assert(!HasReset<std::shared_ptr<int[5]>, int(*)[5]>::value, ""); |
| 55 |
#endif |
| 56 |
|
| 57 |
int main(int, char**) |
| 58 |
{ |
| 59 |
{ |
| 60 |
std::shared_ptr<B> p(new B); |
| 61 |
A* ptr = new A; |
| 62 |
p.reset(ptr); |
| 63 |
assert(A::count == 1); |
| 64 |
assert(B::count == 1); |
| 65 |
assert(p.use_count() == 1); |
| 66 |
assert(p.get() == ptr); |
| 67 |
} |
| 68 |
assert(A::count == 0); |
| 69 |
{ |
| 70 |
std::shared_ptr<B> p; |
| 71 |
A* ptr = new A; |
| 72 |
p.reset(ptr); |
| 73 |
assert(A::count == 1); |
| 74 |
assert(B::count == 1); |
| 75 |
assert(p.use_count() == 1); |
| 76 |
assert(p.get() == ptr); |
| 77 |
} |
| 78 |
assert(A::count == 0); |
| 79 |
|
| 80 |
#if TEST_STD_VER > 14 |
| 81 |
{ |
| 82 |
std::shared_ptr<const A[]> p; |
| 83 |
A* ptr = new A[8]; |
| 84 |
p.reset(ptr); |
| 85 |
assert(A::count == 8); |
| 86 |
assert(p.use_count() == 1); |
| 87 |
assert(p.get() == ptr); |
| 88 |
} |
| 89 |
assert(A::count == 0); |
| 90 |
#endif |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |