blob: 4044afbf0c54d7f36273397b7b62cd9bfc817dea [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// shared_ptr(shared_ptr&& r);
15
16#include <memory>
17#include <cassert>
18
19struct A
20{
21 static int count;
22
23 A() {++count;}
24 A(const A&) {++count;}
25 ~A() {--count;}
26};
27
28int A::count = 0;
29
30int main()
31{
32 {
33 std::shared_ptr<A> pA(new A);
34 assert(pA.use_count() == 1);
35 assert(A::count == 1);
36 {
37 A* p = pA.get();
38 std::shared_ptr<A> pA2(std::move(pA));
39 assert(A::count == 1);
Howard Hinnant73d21a42010-09-04 23:28:19 +000040#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000041 assert(pA.use_count() == 0);
42 assert(pA2.use_count() == 1);
Howard Hinnant73d21a42010-09-04 23:28:19 +000043#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000044 assert(pA.use_count() == 2);
45 assert(pA2.use_count() == 2);
Howard Hinnant73d21a42010-09-04 23:28:19 +000046#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000047 assert(pA2.get() == p);
48 }
Howard Hinnant73d21a42010-09-04 23:28:19 +000049#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000050 assert(pA.use_count() == 0);
51 assert(A::count == 0);
Howard Hinnant73d21a42010-09-04 23:28:19 +000052#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000053 assert(pA.use_count() == 1);
54 assert(A::count == 1);
Howard Hinnant73d21a42010-09-04 23:28:19 +000055#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +000056 }
57 assert(A::count == 0);
58 {
59 std::shared_ptr<A> pA;
60 assert(pA.use_count() == 0);
61 assert(A::count == 0);
62 {
63 std::shared_ptr<A> pA2(std::move(pA));
64 assert(A::count == 0);
65 assert(pA.use_count() == 0);
66 assert(pA2.use_count() == 0);
67 assert(pA2.get() == pA.get());
68 }
69 assert(pA.use_count() == 0);
70 assert(A::count == 0);
71 }
72 assert(A::count == 0);
73}