]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/move_ctor.cc
1 // { dg-do run { target c++14 } }
3 // Copyright (C) 2015-2023 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // 8.2.1 Class template shared_ptr [memory.smartptr.shared]
22 #include <experimental/memory>
23 #include <testsuite_hooks.h>
28 virtual ~A() { ++dtor_count
; }
29 static long ctor_count
;
30 static long dtor_count
;
32 long A::ctor_count
= 0;
33 long A::dtor_count
= 0;
37 void operator()(A
* p
) const { delete [] p
; ++delete_count
; }
38 static long delete_count
;
40 long D::delete_count
= 0;
42 struct reset_count_struct
52 // 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const]
54 // Rvalue construction
57 reset_count_struct
__attribute__((unused
)) reset
;
59 std::experimental::shared_ptr
<A
[5]> a1
;
60 std::experimental::shared_ptr
<A
[5]> a2(std::move(a1
));
61 VERIFY( a1
.use_count() == 0 );
62 VERIFY( a2
.use_count() == 0 );
63 VERIFY( A::ctor_count
== 0 );
64 VERIFY( A::dtor_count
== 0 );
72 reset_count_struct
__attribute__((unused
)) reset
;
74 std::experimental::shared_ptr
<A
[5]> a1(new A
[5]);
75 std::experimental::shared_ptr
<A
[5]> a2(std::move(a1
));
76 VERIFY( a2
.use_count() == 1 );
77 VERIFY( A::ctor_count
== 5 );
78 VERIFY( A::dtor_count
== 0 );
86 reset_count_struct
__attribute__((unused
)) reset
;
88 std::experimental::shared_ptr
<A
[5]> b(new A
[5], D());
89 std::experimental::shared_ptr
<A
[5]> b1(std::move(b
));
90 VERIFY( b
.use_count() == 0 );
91 VERIFY( b1
.use_count() == 1 );
92 VERIFY( A::ctor_count
== 5 );
93 VERIFY( A::dtor_count
== 0 );
95 b1
= std::move(std::experimental::shared_ptr
<A
[5]> ());
97 VERIFY( A::ctor_count
== 5 );
98 VERIFY( A::dtor_count
== 5 );
99 VERIFY( D::delete_count
== 1 );
107 reset_count_struct
__attribute__((unused
)) reset
;
109 std::experimental::shared_ptr
<A
[5]> a(std::move(std::experimental
113 VERIFY( a
.use_count() == 1 );
114 VERIFY( A::ctor_count
== 5 );
115 VERIFY( A::dtor_count
== 0 );
121 reset_count_struct
__attribute__((unused
)) reset
;
123 std::experimental::shared_ptr
<A
[]> a(std::move(std::experimental
127 VERIFY( a
.use_count() == 1 );
128 VERIFY( A::ctor_count
== 5 );
129 VERIFY( A::dtor_count
== 0 );