Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <memory> |
| 11 | |
| 12 | // shared_ptr |
| 13 | |
| 14 | // template<class Y> explicit shared_ptr(const weak_ptr<Y>& r); |
| 15 | |
| 16 | #include <memory> |
| 17 | #include <cassert> |
| 18 | |
| 19 | struct B |
| 20 | { |
| 21 | static int count; |
| 22 | |
| 23 | B() {++count;} |
| 24 | B(const B&) {++count;} |
| 25 | virtual ~B() {--count;} |
| 26 | }; |
| 27 | |
| 28 | int B::count = 0; |
| 29 | |
| 30 | struct A |
| 31 | : public B |
| 32 | { |
| 33 | static int count; |
| 34 | |
| 35 | A() {++count;} |
| 36 | A(const A&) {++count;} |
| 37 | ~A() {--count;} |
| 38 | }; |
| 39 | |
| 40 | int A::count = 0; |
| 41 | |
| 42 | int main() |
| 43 | { |
| 44 | { |
| 45 | std::weak_ptr<A> wp; |
| 46 | try |
| 47 | { |
| 48 | std::shared_ptr<A> sp(wp); |
| 49 | assert(false); |
| 50 | } |
| 51 | catch (std::bad_weak_ptr&) |
| 52 | { |
| 53 | } |
| 54 | assert(A::count == 0); |
| 55 | } |
| 56 | { |
| 57 | std::shared_ptr<A> sp0(new A); |
| 58 | std::weak_ptr<A> wp(sp0); |
| 59 | std::shared_ptr<A> sp(wp); |
| 60 | assert(sp.use_count() == 2); |
| 61 | assert(sp.get() == sp0.get()); |
| 62 | assert(A::count == 1); |
| 63 | } |
| 64 | assert(A::count == 0); |
| 65 | { |
| 66 | std::shared_ptr<A> sp0(new A); |
| 67 | std::weak_ptr<A> wp(sp0); |
| 68 | sp0.reset(); |
| 69 | try |
| 70 | { |
| 71 | std::shared_ptr<A> sp(wp); |
| 72 | assert(false); |
| 73 | } |
| 74 | catch (std::bad_weak_ptr&) |
| 75 | { |
| 76 | } |
| 77 | } |
| 78 | assert(A::count == 0); |
| 79 | } |