]> git.ipfire.org Git - thirdparty/gcc.git/blame - 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
CommitLineData
52066eae 1// { dg-do run { target c++11 } }
aaf0ca6f 2
99dee823 3// Copyright (C) 2008-2021 Free Software Foundation, Inc.
aaf0ca6f
JW
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
aaf0ca6f
JW
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
748086b7
JJ
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
aaf0ca6f 19
23df8534 20// 20.7.2.2 Class template shared_ptr [util.smartptr.shared]
aaf0ca6f
JW
21
22#include <memory>
23#include <testsuite_hooks.h>
24
25struct A { };
aaf0ca6f 26
a2e0054e
JW
27int destroyed = 0;
28struct B : A { ~B() { ++destroyed; } };
29
23df8534 30// 20.7.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
aaf0ca6f 31
640cbe76 32// Construction from unique_ptr
a2e0054e
JW
33
34template<typename From, typename To>
35constexpr 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
43static_assert( constructible< A, A >(), "A -> A compatible" );
44static_assert( constructible< B, A >(), "B -> A compatible" );
45static_assert( constructible< int, int >(), "int -> int compatible" );
46static_assert( !constructible< int, long >(), "int -> long not compatible" );
47
48void
aaf0ca6f
JW
49test01()
50{
640cbe76
JW
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 );
a2e0054e 56}
aaf0ca6f 57
a2e0054e
JW
58void
59test02()
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 );
aaf0ca6f
JW
68}
69
640cbe76 70int
aaf0ca6f
JW
71main()
72{
73 test01();
a2e0054e 74 test02();
aaf0ca6f
JW
75 return 0;
76}