| 1 | // Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++11 } } |
| 19 | // { dg-require-effective-target hosted } |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | template<typename T> |
| 25 | bool not_enabled(T& t) |
| 26 | { |
| 27 | #if __cpp_lib_enable_shared_from_this >= 201603 |
| 28 | return t.weak_from_this().expired(); |
| 29 | #else |
| 30 | try { |
| 31 | t.shared_from_this(); |
| 32 | return false; |
| 33 | } catch (const std::bad_weak_ptr&) { |
| 34 | return true; |
| 35 | } |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | struct A : std::enable_shared_from_this<A> { }; |
| 40 | struct B : std::enable_shared_from_this<B> { }; |
| 41 | struct D : A, B { }; |
| 42 | |
| 43 | void test01() |
| 44 | { |
| 45 | auto d = std::make_shared<D>(); |
| 46 | VERIFY( not_enabled( static_cast<A&>(*d) ) ); |
| 47 | VERIFY( not_enabled( static_cast<const A&>(*d) ) ); |
| 48 | VERIFY( not_enabled( static_cast<B&>(*d) ) ); |
| 49 | VERIFY( not_enabled( static_cast<const B&>(*d) ) ); |
| 50 | } |
| 51 | |
| 52 | struct E : std::__enable_shared_from_this<E> { }; |
| 53 | struct F : std::__enable_shared_from_this<F> { }; |
| 54 | struct G : E, F { }; |
| 55 | |
| 56 | void test02() |
| 57 | { |
| 58 | auto g = std::make_shared<G>(); |
| 59 | VERIFY( not_enabled( static_cast<E&>(*g) ) ); |
| 60 | VERIFY( not_enabled( static_cast<const E&>(*g) ) ); |
| 61 | VERIFY( not_enabled( static_cast<F&>(*g) ) ); |
| 62 | VERIFY( not_enabled( static_cast<const F&>(*g) ) ); |
| 63 | } |
| 64 | |
| 65 | struct H : D, G { }; |
| 66 | |
| 67 | void test03() |
| 68 | { |
| 69 | auto h = std::make_shared<H>(); |
| 70 | VERIFY( not_enabled( static_cast<A&>(*h) ) ); |
| 71 | VERIFY( not_enabled( static_cast<const A&>(*h) ) ); |
| 72 | VERIFY( not_enabled( static_cast<B&>(*h) ) ); |
| 73 | VERIFY( not_enabled( static_cast<const B&>(*h) ) ); |
| 74 | VERIFY( not_enabled( static_cast<E&>(*h) ) ); |
| 75 | VERIFY( not_enabled( static_cast<const E&>(*h) ) ); |
| 76 | VERIFY( not_enabled( static_cast<F&>(*h) ) ); |
| 77 | VERIFY( not_enabled( static_cast<const F&>(*h) ) ); |
| 78 | } |
| 79 | |
| 80 | int |
| 81 | main() |
| 82 | { |
| 83 | test01(); |
| 84 | test02(); |
| 85 | test03(); |
| 86 | } |