Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <memory> |
| 11 | |
| 12 | // shared_ptr |
| 13 | |
| 14 | // template<class Y> shared_ptr(shared_ptr<Y>&& r); |
| 15 | |
| 16 | #include <memory> |
| 17 | #include <type_traits> |
| 18 | #include <cassert> |
| 19 | |
| 20 | struct B |
| 21 | { |
| 22 | static int count; |
| 23 | |
| 24 | B() {++count;} |
| 25 | B(const B&) {++count;} |
| 26 | virtual ~B() {--count;} |
| 27 | }; |
| 28 | |
| 29 | int B::count = 0; |
| 30 | |
| 31 | struct A |
| 32 | : public B |
| 33 | { |
| 34 | static int count; |
| 35 | |
| 36 | A() {++count;} |
| 37 | A(const A&) {++count;} |
| 38 | ~A() {--count;} |
| 39 | }; |
| 40 | |
| 41 | int A::count = 0; |
| 42 | |
| 43 | struct C |
| 44 | { |
| 45 | static int count; |
| 46 | |
| 47 | C() {++count;} |
| 48 | C(const C&) {++count;} |
| 49 | virtual ~C() {--count;} |
| 50 | }; |
| 51 | |
| 52 | int C::count = 0; |
| 53 | |
| 54 | int main() |
| 55 | { |
| 56 | static_assert(( std::is_convertible<std::shared_ptr<A>, std::shared_ptr<B> >::value), ""); |
| 57 | static_assert((!std::is_convertible<std::shared_ptr<B>, std::shared_ptr<A> >::value), ""); |
| 58 | static_assert((!std::is_convertible<std::shared_ptr<A>, std::shared_ptr<C> >::value), ""); |
| 59 | { |
| 60 | std::shared_ptr<A> pA(new A); |
| 61 | assert(pA.use_count() == 1); |
| 62 | assert(B::count == 1); |
| 63 | assert(A::count == 1); |
| 64 | { |
| 65 | B* p = pA.get(); |
| 66 | std::shared_ptr<B> pB(std::move(pA)); |
| 67 | assert(B::count == 1); |
| 68 | assert(A::count == 1); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 69 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 70 | assert(pB.use_count() == 1); |
| 71 | assert(pA.use_count() == 0); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 72 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 73 | assert(pB.use_count() == 2); |
| 74 | assert(pA.use_count() == 2); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 75 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 76 | assert(p == pB.get()); |
| 77 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 78 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 79 | assert(pA.use_count() == 0); |
| 80 | assert(B::count == 0); |
| 81 | assert(A::count == 0); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 82 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 83 | assert(pA.use_count() == 1); |
| 84 | assert(B::count == 1); |
| 85 | assert(A::count == 1); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 86 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 87 | } |
| 88 | assert(B::count == 0); |
| 89 | assert(A::count == 0); |
| 90 | { |
| 91 | std::shared_ptr<A> pA; |
| 92 | assert(pA.use_count() == 0); |
| 93 | assert(B::count == 0); |
| 94 | assert(A::count == 0); |
| 95 | { |
| 96 | std::shared_ptr<B> pB(pA); |
| 97 | assert(B::count == 0); |
| 98 | assert(A::count == 0); |
| 99 | assert(pB.use_count() == 0); |
| 100 | assert(pA.use_count() == 0); |
| 101 | assert(pA.get() == pB.get()); |
| 102 | } |
| 103 | assert(pA.use_count() == 0); |
| 104 | assert(B::count == 0); |
| 105 | assert(A::count == 0); |
| 106 | } |
| 107 | assert(B::count == 0); |
| 108 | assert(A::count == 0); |
| 109 | } |