]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/move.cc
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
4 // Copyright (C) 2007-2024 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 // TR1 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
25 #include <testsuite_hooks.h>
30 virtual ~A() { ++dtor_count
; }
31 static long ctor_count
;
32 static long dtor_count
;
34 long A::ctor_count
= 0;
35 long A::dtor_count
= 0;
40 virtual ~B() { ++dtor_count
; }
41 static long ctor_count
;
42 static long dtor_count
;
44 long B::ctor_count
= 0;
45 long B::dtor_count
= 0;
49 void operator()(B
* p
) const { delete p
; ++delete_count
; }
50 static long delete_count
;
52 long D::delete_count
= 0;
54 struct reset_count_struct
66 // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
68 // Rvalue construction
71 reset_count_struct
__attribute__((unused
)) reset
;
73 std::shared_ptr
<A
> a1
;
74 std::shared_ptr
<A
> a2(std::move(a1
));
75 VERIFY( a1
.use_count() == 0 );
76 VERIFY( a2
.use_count() == 0 );
77 VERIFY( A::ctor_count
== 0 );
78 VERIFY( A::dtor_count
== 0 );
79 VERIFY( B::ctor_count
== 0 );
80 VERIFY( B::dtor_count
== 0 );
88 reset_count_struct
__attribute__((unused
)) reset
;
90 std::shared_ptr
<A
> a1(new A
);
91 std::shared_ptr
<A
> a2(std::move(a1
));
92 VERIFY( a1
.use_count() == 0 );
93 VERIFY( a2
.use_count() == 1 );
94 VERIFY( A::ctor_count
== 1 );
95 VERIFY( A::dtor_count
== 0 );
103 reset_count_struct
__attribute__((unused
)) reset
;
105 std::shared_ptr
<B
> b(new B
);
106 std::shared_ptr
<A
> a(std::move(b
));
107 VERIFY( b
.use_count() == 0 );
108 VERIFY( a
.use_count() == 1 );
109 VERIFY( A::ctor_count
== 1 );
110 VERIFY( A::dtor_count
== 0 );
111 VERIFY( B::ctor_count
== 1 );
112 VERIFY( B::dtor_count
== 0 );
120 reset_count_struct
__attribute__((unused
)) reset
;
122 std::shared_ptr
<B
> b(new B
, D());
123 std::shared_ptr
<A
> a(std::move(b
));
124 VERIFY( b
.use_count() == 0 );
125 VERIFY( a
.use_count() == 1 );
126 VERIFY( A::ctor_count
== 1 );
127 VERIFY( A::dtor_count
== 0 );
128 VERIFY( B::ctor_count
== 1 );
129 VERIFY( B::dtor_count
== 0 );
131 a
= std::move(std::shared_ptr
<A
>());
132 VERIFY( D::delete_count
== 1 );
133 VERIFY( B::dtor_count
== 1 );
141 reset_count_struct
__attribute__((unused
)) reset
;
143 std::shared_ptr
<A
> a(std::move(std::shared_ptr
<A
>(new A
)));
144 VERIFY( a
.use_count() == 1 );
145 VERIFY( A::ctor_count
== 1 );
146 VERIFY( A::dtor_count
== 0 );