blob: b2e61faff5ed90220a43c2333e2871f0faec9444 [file] [log] [blame]
Howard Hinnantc52f43e2010-08-22 00:59:46 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc52f43e2010-08-22 00:59:46 +00007//
8//===----------------------------------------------------------------------===//
9
10// <memory>
11
12// template<class Y> explicit shared_ptr(auto_ptr<Y>&& r);
13
Eric Fiselier07a4bec2015-03-10 20:46:04 +000014// UNSUPPORTED: sanitizer-new-delete
Eric Fiselier72aab5f2014-11-04 05:11:41 +000015
Howard Hinnantc52f43e2010-08-22 00:59:46 +000016#include <memory>
17#include <new>
18#include <cstdlib>
19#include <cassert>
20
21bool throw_next = false;
22
23void* operator new(std::size_t s) throw(std::bad_alloc)
24{
25 if (throw_next)
26 throw std::bad_alloc();
27 return std::malloc(s);
28}
29
30void operator delete(void* p) throw()
31{
32 std::free(p);
33}
34
35struct B
36{
37 static int count;
38
39 B() {++count;}
40 B(const B&) {++count;}
41 virtual ~B() {--count;}
42};
43
44int B::count = 0;
45
46struct A
47 : public B
48{
49 static int count;
50
51 A() {++count;}
52 A(const A&) {++count;}
53 ~A() {--count;}
54};
55
56int A::count = 0;
57
58int main()
59{
60 {
61 std::auto_ptr<A> ptr(new A);
62 A* raw_ptr = ptr.get();
Howard Hinnant73d21a42010-09-04 23:28:19 +000063#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000064 std::shared_ptr<B> p(std::move(ptr));
65#else
66 std::shared_ptr<B> p(ptr);
67#endif
68 assert(A::count == 1);
69 assert(B::count == 1);
70 assert(p.use_count() == 1);
71 assert(p.get() == raw_ptr);
72 assert(ptr.get() == 0);
73 }
74 assert(A::count == 0);
75 {
76 std::auto_ptr<A> ptr(new A);
77 A* raw_ptr = ptr.get();
78 throw_next = true;
79 try
80 {
Howard Hinnant73d21a42010-09-04 23:28:19 +000081#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000082 std::shared_ptr<B> p(std::move(ptr));
83#else
84 std::shared_ptr<B> p(ptr);
85#endif
86 assert(false);
87 }
88 catch (...)
89 {
Sean Hunt541cb302011-07-18 23:51:25 +000090#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000091 assert(A::count == 1);
92 assert(B::count == 1);
93 assert(ptr.get() == raw_ptr);
Sean Hunt541cb302011-07-18 23:51:25 +000094#else
95 // Without rvalue references, ptr got copied into
96 // the shared_ptr destructor and the copy was
97 // destroyed during unwinding.
98 assert(A::count == 0);
99 assert(B::count == 0);
100#endif
Howard Hinnantc52f43e2010-08-22 00:59:46 +0000101 }
102 }
103 assert(A::count == 0);
104}