]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/shared_ptr/cons/unique_ptr_array.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / unique_ptr_array.cc
CommitLineData
52066eae 1// { dg-do run { target c++11 } }
23df8534 2
a5544970 3// Copyright (C) 2012-2019 Free Software Foundation, Inc.
23df8534
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
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
b2343559
JW
25#if __cpp_lib_shared_ptr_arrays >= 201603
26# define SHARED_PTR_ARRAYS
27#endif
28#if __cpp_lib_enable_shared_from_this >= 201603
29# define WEAK_FROM_THIS
30#endif
31
23df8534
JW
32int destroyed = 0;
33
34struct A : std::enable_shared_from_this<A>
35{
36 ~A() { ++destroyed; }
37};
38
39// 20.7.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
40
41// Construction from unique_ptr<A[]>
42int
43test01()
44{
23df8534 45 std::unique_ptr<A[]> up(new A[2]);
b2343559
JW
46#ifdef SHARED_PTR_ARRAYS
47 std::shared_ptr<A[]> sp(std::move(up));
48#else
23df8534 49 std::shared_ptr<A> sp(std::move(up));
b2343559 50#endif
23df8534
JW
51 VERIFY( up.get() == 0 );
52 VERIFY( sp.get() != 0 );
53 VERIFY( sp.use_count() == 1 );
54
b2343559
JW
55#ifdef SHARED_PTR_ARRAYS
56# ifdef WEAK_FROM_THIS
57 VERIFY( sp[0].weak_from_this().expired() );
58# endif
59#else
23df8534 60 VERIFY( sp->shared_from_this() != nullptr );
b2343559 61#endif
23df8534
JW
62
63 sp.reset();
64 VERIFY( destroyed == 2 );
65
66 return 0;
67}
68
69int
70main()
71{
72 test01();
73 return 0;
74}