blob: 4ccad4e0aab9c83e5121ffbd31022b90fd60694a [file] [log] [blame]
Howard Hinnantc52f43e2010-08-22 00:59:46 +00001//===----------------------------------------------------------------------===//
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// template<class Y> shared_ptr(shared_ptr<Y>&& r);
15
16#include <memory>
17#include <type_traits>
18#include <cassert>
19
20struct B
21{
22 static int count;
23
24 B() {++count;}
25 B(const B&) {++count;}
26 virtual ~B() {--count;}
27};
28
29int B::count = 0;
30
31struct A
32 : public B
33{
34 static int count;
35
36 A() {++count;}
37 A(const A&) {++count;}
38 ~A() {--count;}
39};
40
41int A::count = 0;
42
43struct C
44{
45 static int count;
46
47 C() {++count;}
48 C(const C&) {++count;}
49 virtual ~C() {--count;}
50};
51
52int C::count = 0;
53
54int main()
55{
56 static_assert(( std::is_convertible<std::shared_ptr<A>, std::shared_ptr<B> >::value), "");
57 static_assert((!std::is_convertible<std::shared_ptr<B>, std::shared_ptr<A> >::value), "");
58 static_assert((!std::is_convertible<std::shared_ptr<A>, std::shared_ptr<C> >::value), "");
59 {
60 std::shared_ptr<A> pA(new A);
61 assert(pA.use_count() == 1);
62 assert(B::count == 1);
63 assert(A::count == 1);
64 {
65 B* p = pA.get();
66 std::shared_ptr<B> pB(std::move(pA));
67 assert(B::count == 1);
68 assert(A::count == 1);
Howard Hinnant73d21a42010-09-04 23:28:19 +000069#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000070 assert(pB.use_count() == 1);
71 assert(pA.use_count() == 0);
Howard Hinnant73d21a42010-09-04 23:28:19 +000072#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000073 assert(pB.use_count() == 2);
74 assert(pA.use_count() == 2);
Howard Hinnant73d21a42010-09-04 23:28:19 +000075#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000076 assert(p == pB.get());
77 }
Howard Hinnant73d21a42010-09-04 23:28:19 +000078#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000079 assert(pA.use_count() == 0);
80 assert(B::count == 0);
81 assert(A::count == 0);
Howard Hinnant73d21a42010-09-04 23:28:19 +000082#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000083 assert(pA.use_count() == 1);
84 assert(B::count == 1);
85 assert(A::count == 1);
Howard Hinnant73d21a42010-09-04 23:28:19 +000086#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000087 }
88 assert(B::count == 0);
89 assert(A::count == 0);
90 {
91 std::shared_ptr<A> pA;
92 assert(pA.use_count() == 0);
93 assert(B::count == 0);
94 assert(A::count == 0);
95 {
96 std::shared_ptr<B> pB(pA);
97 assert(B::count == 0);
98 assert(A::count == 0);
99 assert(pB.use_count() == 0);
100 assert(pA.use_count() == 0);
101 assert(pA.get() == pB.get());
102 }
103 assert(pA.use_count() == 0);
104 assert(B::count == 0);
105 assert(A::count == 0);
106 }
107 assert(B::count == 0);
108 assert(A::count == 0);
109}