1 // { dg-do run { target c++20 } }
2 // { dg-require-effective-target hosted }
4 // Copyright (C) 2019-2023 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 // Template class shared_ptr [util.smartptr.shared]
24 #include <testsuite_hooks.h>
40 // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
42 // Aliasing constructors
50 std::shared_ptr
<bool> b1(std::move(a
), &test
);
51 VERIFY( b1
.use_count() == 0 );
52 VERIFY( b1
.get() == &test
);
53 VERIFY( a
.use_count() == 0 );
54 VERIFY( a
== nullptr );
56 std::shared_ptr
<bool> b2(b1
);
57 VERIFY( b2
.use_count() == 0 );
66 std::shared_ptr
<A
> a(ptr
);
67 std::shared_ptr
<int> i1(std::move(a
), &a
->i
);
68 VERIFY( i1
.use_count() == 1 );
70 VERIFY( a
.use_count() == 0 );
71 VERIFY( a
== nullptr );
73 std::shared_ptr
<int> i2(i1
);
74 VERIFY( i2
.use_count() == 2 );
75 VERIFY( i2
.get() == &ptr
->i
);
81 std::shared_ptr
<B
> b1(new B
);
84 std::shared_ptr
<B
> b2(b1
);
85 std::shared_ptr
<A
> a1(std::move(b1
), b1
.get());
86 std::shared_ptr
<A
> a2(std::move(b2
), &b2
->a
);
87 VERIFY( a1
.use_count() == 2 );
88 VERIFY( a2
.use_count() == 2 );
89 VERIFY( a1
->i
== 100 );
90 VERIFY( a2
->i
== 200 );
91 VERIFY( b1
.use_count() == 0 );
92 VERIFY( b2
.use_count() == 0 );
93 VERIFY( b1
== nullptr );
94 VERIFY( b2
== nullptr );