1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <memory>
12 
13 // shared_ptr
14 
15 // shared_ptr(shared_ptr&& r);
16 
17 #include <memory>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 
22 struct A
23 {
24     static int count;
25 
AA26     A() {++count;}
AA27     A(const A&) {++count;}
~AA28     ~A() {--count;}
29 };
30 
31 int A::count = 0;
32 
main(int,char **)33 int main(int, char**)
34 {
35     {
36         std::shared_ptr<A> pA(new A);
37         assert(pA.use_count() == 1);
38         assert(A::count == 1);
39         {
40             A* p = pA.get();
41             std::shared_ptr<A> pA2(std::move(pA));
42             assert(A::count == 1);
43 #if TEST_STD_VER >= 11
44             assert(pA.use_count() == 0);
45             assert(pA2.use_count() == 1);
46 #else
47             assert(pA.use_count() == 2);
48             assert(pA2.use_count() == 2);
49 #endif
50             assert(pA2.get() == p);
51         }
52 #if TEST_STD_VER >= 11
53         assert(pA.use_count() == 0);
54         assert(A::count == 0);
55 #else
56         assert(pA.use_count() == 1);
57         assert(A::count == 1);
58 #endif
59     }
60 
61     assert(A::count == 0);
62     {
63         std::shared_ptr<A> pA;
64         assert(pA.use_count() == 0);
65         assert(A::count == 0);
66         {
67             std::shared_ptr<A> pA2(std::move(pA));
68             assert(A::count == 0);
69             assert(pA.use_count() == 0);
70             assert(pA2.use_count() == 0);
71             assert(pA2.get() == pA.get());
72         }
73         assert(pA.use_count() == 0);
74         assert(A::count == 0);
75     }
76     assert(A::count == 0);
77 
78     {
79         std::shared_ptr<A const> pA(new A);
80         A const* p = pA.get();
81         std::shared_ptr<A const> pA2(std::move(pA));
82         assert(pA2.get() == p);
83     }
84 
85   return 0;
86 }
87