]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/unique_ptr.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / unique_ptr.cc
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2008-2022 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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/>.
19
20 // 20.7.2.2 Class template shared_ptr [util.smartptr.shared]
21
22 #include <memory>
23 #include <testsuite_hooks.h>
24
25 struct A { };
26
27 int destroyed = 0;
28 struct B : A { ~B() { ++destroyed; } };
29
30 // 20.7.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
31
32 // Construction from unique_ptr
33
34 template<typename From, typename To>
35 constexpr bool constructible()
36 {
37 using namespace std;
38 return is_constructible<shared_ptr<To>, unique_ptr<From>>::value
39 && is_constructible<shared_ptr<const To>, unique_ptr<From>>::value
40 && is_constructible<shared_ptr<const To>, unique_ptr<const From>>::value;
41 }
42
43 static_assert( constructible< A, A >(), "A -> A compatible" );
44 static_assert( constructible< B, A >(), "B -> A compatible" );
45 static_assert( constructible< int, int >(), "int -> int compatible" );
46 static_assert( !constructible< int, long >(), "int -> long not compatible" );
47
48 void
49 test01()
50 {
51 std::unique_ptr<A> up(new A);
52 std::shared_ptr<A> sp(std::move(up));
53 VERIFY( up.get() == 0 );
54 VERIFY( sp.get() != 0 );
55 VERIFY( sp.use_count() == 1 );
56 }
57
58 void
59 test02()
60 {
61 std::unique_ptr<B> b(new B);
62 std::shared_ptr<A> a(std::move(b));
63 VERIFY( b.get() == 0 );
64 VERIFY( a.get() != 0 );
65 VERIFY( a.use_count() == 1 );
66 a.reset();
67 VERIFY( destroyed == 1 );
68 }
69
70 int
71 main()
72 {
73 test01();
74 test02();
75 return 0;
76 }