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 // template<class Y> explicit shared_ptr(auto_ptr<Y>&& r); 13 // REQUIRES: c++98 || c++03 || c++11 || c++14 14 15 16 #include <memory> 17 #include <new> 18 #include <cstdlib> 19 #include <cassert> 20 21 #include "test_macros.h" 22 #include "count_new.hpp" 23 24 struct B 25 { 26 static int count; 27 28 B() {++count;} 29 B(const B&) {++count;} 30 virtual ~B() {--count;} 31 }; 32 33 int B::count = 0; 34 35 struct A 36 : public B 37 { 38 static int count; 39 40 A() {++count;} 41 A(const A&) {++count;} 42 ~A() {--count;} 43 }; 44 45 int A::count = 0; 46 47 int main() 48 { 49 { 50 std::auto_ptr<A> ptr(new A); 51 A* raw_ptr = ptr.get(); 52 #if TEST_STD_VER >= 11 53 std::shared_ptr<B> p(std::move(ptr)); 54 #else 55 std::shared_ptr<B> p(ptr); 56 #endif 57 assert(A::count == 1); 58 assert(B::count == 1); 59 assert(p.use_count() == 1); 60 assert(p.get() == raw_ptr); 61 assert(ptr.get() == 0); 62 } 63 assert(A::count == 0); 64 assert(globalMemCounter.checkOutstandingNewEq(0)); 65 #if !defined(TEST_HAS_NO_EXCEPTIONS) && !defined(DISABLE_NEW_COUNT) 66 { 67 std::auto_ptr<A> ptr(new A); 68 A* raw_ptr = ptr.get(); 69 globalMemCounter.throw_after = 0; 70 try 71 { 72 #if TEST_STD_VER >= 11 73 std::shared_ptr<B> p(std::move(ptr)); 74 #else 75 std::shared_ptr<B> p(ptr); 76 #endif 77 assert(false); 78 } 79 catch (...) 80 { 81 #if TEST_STD_VER >= 11 82 assert(A::count == 1); 83 assert(B::count == 1); 84 assert(ptr.get() == raw_ptr); 85 #else 86 // Without rvalue references, ptr got copied into 87 // the shared_ptr destructor and the copy was 88 // destroyed during unwinding. 89 (void) raw_ptr; // silence 'unused variable' warning 90 assert(A::count == 0); 91 assert(B::count == 0); 92 #endif 93 } 94 } 95 assert(A::count == 0); 96 assert(globalMemCounter.checkOutstandingNewEq(0)); 97 #endif // !defined(TEST_HAS_NO_EXCEPTIONS) && !defined(DISABLE_NEW_COUNT) 98 } 99