| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 5 | // |
| 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) |
| 10 | // any later version. |
| 11 | |
| 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. |
| 16 | |
| 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/>. |
| 20 | |
| 21 | // 20.7.2.2 Class template shared_ptr [util.smartptr.shared] |
| 22 | |
| 23 | #include <memory> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | #if __cpp_lib_shared_ptr_arrays >= 201603 |
| 27 | # define SHARED_PTR_ARRAYS |
| 28 | #endif |
| 29 | #if __cpp_lib_enable_shared_from_this >= 201603 |
| 30 | # define WEAK_FROM_THIS |
| 31 | #endif |
| 32 | |
| 33 | int destroyed = 0; |
| 34 | |
| 35 | struct A : std::enable_shared_from_this<A> |
| 36 | { |
| 37 | ~A() { ++destroyed; } |
| 38 | }; |
| 39 | |
| 40 | // 20.7.2.2.1 shared_ptr constructors [util.smartptr.shared.const] |
| 41 | |
| 42 | // Construction from unique_ptr<A[]> |
| 43 | int |
| 44 | test01() |
| 45 | { |
| 46 | std::unique_ptr<A[]> up(new A[2]); |
| 47 | #ifdef SHARED_PTR_ARRAYS |
| 48 | std::shared_ptr<A[]> sp(std::move(up)); |
| 49 | #else |
| 50 | std::shared_ptr<A> sp(std::move(up)); |
| 51 | #endif |
| 52 | VERIFY( up.get() == 0 ); |
| 53 | VERIFY( sp.get() != 0 ); |
| 54 | VERIFY( sp.use_count() == 1 ); |
| 55 | |
| 56 | #ifdef SHARED_PTR_ARRAYS |
| 57 | # ifdef WEAK_FROM_THIS |
| 58 | VERIFY( sp[0].weak_from_this().expired() ); |
| 59 | # endif |
| 60 | #else |
| 61 | VERIFY( sp->shared_from_this() != nullptr ); |
| 62 | #endif |
| 63 | |
| 64 | sp.reset(); |
| 65 | VERIFY( destroyed == 2 ); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int |
| 71 | main() |
| 72 | { |
| 73 | test01(); |
| 74 | return 0; |
| 75 | } |