| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // Copyright (C) 2008-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.8.13.2 Template class shared_ptr [util.smartptr.shared] |
| 22 | |
| 23 | #include <memory> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | struct A |
| 27 | { |
| 28 | int i; |
| 29 | virtual ~A() { } |
| 30 | }; |
| 31 | |
| 32 | struct B : A |
| 33 | { |
| 34 | }; |
| 35 | |
| 36 | // 20.6.6.2.5 shared_ptr observers [util.smartptr.shared.obs] |
| 37 | |
| 38 | void |
| 39 | test01() |
| 40 | { |
| 41 | // test empty shared_ptrs compare equivalent |
| 42 | std::shared_ptr<A> p1; |
| 43 | std::shared_ptr<B> p2; |
| 44 | VERIFY( !p1.owner_before(p2) && !p2.owner_before(p1) ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | // Construction from pointer |
| 49 | void |
| 50 | test02() |
| 51 | { |
| 52 | std::shared_ptr<A> a0; |
| 53 | |
| 54 | std::shared_ptr<A> a1(new A); |
| 55 | VERIFY( a1.owner_before(a0) || a0.owner_before(a1) ); |
| 56 | VERIFY( !(a1.owner_before(a0) && a0.owner_before(a1)) ); |
| 57 | |
| 58 | std::shared_ptr<B> b1(new B); |
| 59 | VERIFY( a1.owner_before(b1) || b1.owner_before(a1) ); |
| 60 | VERIFY( !(a1.owner_before(b1) && b1.owner_before(a1)) ); |
| 61 | |
| 62 | std::shared_ptr<A> a2(a1); |
| 63 | VERIFY( !a1.owner_before(a2) && !a2.owner_before(a1) ); |
| 64 | a2 = b1; |
| 65 | VERIFY( !b1.owner_before(a2) && !a2.owner_before(b1) ); |
| 66 | |
| 67 | std::weak_ptr<A> w1(a1); |
| 68 | VERIFY( !a1.owner_before(w1) && !w1.owner_before(a1) ); |
| 69 | std::weak_ptr<A> w2(a2); |
| 70 | VERIFY( !b1.owner_before(w2) && !w2.owner_before(b1) ); |
| 71 | |
| 72 | static_assert( noexcept(a1.owner_before(a0)), "" ); |
| 73 | static_assert( noexcept(a1.owner_before(b1)), "" ); |
| 74 | static_assert( noexcept(b1.owner_before(a1)), "" ); |
| 75 | static_assert( noexcept(a1.owner_before(w1)), "" ); |
| 76 | static_assert( noexcept(b1.owner_before(w1)), "" ); |
| 77 | } |
| 78 | |
| 79 | // Aliasing |
| 80 | void |
| 81 | test03() |
| 82 | { |
| 83 | std::shared_ptr<A> p1(new A()); |
| 84 | std::shared_ptr<int> p2(p1, &p1->i); |
| 85 | VERIFY( !p1.owner_before(p2) && !p2.owner_before(p1) ); |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | main() |
| 90 | { |
| 91 | test01(); |
| 92 | test02(); |
| 93 | test03(); |
| 94 | return 0; |
| 95 | } |