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 | // shared_ptr(shared_ptr&& r); |
| 15 | |
| 16 | #include <memory> |
| 17 | #include <cassert> |
| 18 | |
| 19 | struct A |
| 20 | { |
| 21 | static int count; |
| 22 | |
| 23 | A() {++count;} |
| 24 | A(const A&) {++count;} |
| 25 | ~A() {--count;} |
| 26 | }; |
| 27 | |
| 28 | int A::count = 0; |
| 29 | |
| 30 | int main() |
| 31 | { |
| 32 | { |
| 33 | std::shared_ptr<A> pA(new A); |
| 34 | assert(pA.use_count() == 1); |
| 35 | assert(A::count == 1); |
| 36 | { |
| 37 | A* p = pA.get(); |
| 38 | std::shared_ptr<A> pA2(std::move(pA)); |
| 39 | assert(A::count == 1); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 40 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 41 | assert(pA.use_count() == 0); |
| 42 | assert(pA2.use_count() == 1); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 43 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 44 | assert(pA.use_count() == 2); |
| 45 | assert(pA2.use_count() == 2); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 46 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 47 | assert(pA2.get() == p); |
| 48 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 49 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 50 | assert(pA.use_count() == 0); |
| 51 | assert(A::count == 0); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 52 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 53 | assert(pA.use_count() == 1); |
| 54 | assert(A::count == 1); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 55 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 56 | } |
| 57 | assert(A::count == 0); |
| 58 | { |
| 59 | std::shared_ptr<A> pA; |
| 60 | assert(pA.use_count() == 0); |
| 61 | assert(A::count == 0); |
| 62 | { |
| 63 | std::shared_ptr<A> pA2(std::move(pA)); |
| 64 | assert(A::count == 0); |
| 65 | assert(pA.use_count() == 0); |
| 66 | assert(pA2.use_count() == 0); |
| 67 | assert(pA2.get() == pA.get()); |
| 68 | } |
| 69 | assert(pA.use_count() == 0); |
| 70 | assert(A::count == 0); |
| 71 | } |
| 72 | assert(A::count == 0); |
| 73 | } |