1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <memory> 11 12 // weak_ptr 13 14 // template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) 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 A* ptr1 = new A; 34 A* ptr2 = new A; 35 std::shared_ptr<A> p1(ptr1); 36 std::weak_ptr<A> w1(p1); 37 { 38 std::shared_ptr<A> p2(ptr2); 39 std::weak_ptr<A> w2(p2); 40 swap(w1, w2); 41 assert(w1.use_count() == 1); 42 assert(w1.lock().get() == ptr2); 43 assert(w2.use_count() == 1); 44 assert(w2.lock().get() == ptr1); 45 assert(A::count == 2); 46 } 47 } 48 assert(A::count == 0); 49 } 50