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 #include <utility>
20 
21 #include "test_macros.h"
22 
23 struct A
24 {
25     static int count;
26 
AA27     A() {++count;}
AA28     A(const A&) {++count;}
~AA29     ~A() {--count;}
30 };
31 
32 int A::count = 0;
33 
main(int,char **)34 int main(int, char**)
35 {
36     {
37         std::shared_ptr<A> pA(new A);
38         assert(pA.use_count() == 1);
39         assert(A::count == 1);
40         {
41             A* p = pA.get();
42             std::shared_ptr<A> pA2(std::move(pA));
43             assert(A::count == 1);
44 #if TEST_STD_VER >= 11
45             assert(pA.use_count() == 0);
46             assert(pA2.use_count() == 1);
47 #else
48             assert(pA.use_count() == 2);
49             assert(pA2.use_count() == 2);
50 #endif
51             assert(pA2.get() == p);
52         }
53 #if TEST_STD_VER >= 11
54         assert(pA.use_count() == 0);
55         assert(A::count == 0);
56 #else
57         assert(pA.use_count() == 1);
58         assert(A::count == 1);
59 #endif
60     }
61 
62     assert(A::count == 0);
63     {
64         std::shared_ptr<A> pA;
65         assert(pA.use_count() == 0);
66         assert(A::count == 0);
67         {
68             std::shared_ptr<A> pA2(std::move(pA));
69             assert(A::count == 0);
70             assert(pA.use_count() == 0);
71             assert(pA2.use_count() == 0);
72             assert(pA2.get() == pA.get());
73         }
74         assert(pA.use_count() == 0);
75         assert(A::count == 0);
76     }
77     assert(A::count == 0);
78 
79     {
80         std::shared_ptr<A const> pA(new A);
81         A const* p = pA.get();
82         std::shared_ptr<A const> pA2(std::move(pA));
83         assert(pA2.get() == p);
84     }
85 
86   return 0;
87 }
88