| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-options "-g -O0" } |
| 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 | #include <memory> |
| 22 | #include <iostream> |
| 23 | #include <cstdint> |
| 24 | |
| 25 | template<class T> |
| 26 | void |
| 27 | placeholder(const T &s) |
| 28 | { |
| 29 | std::cout << s; |
| 30 | } |
| 31 | |
| 32 | template<class T> |
| 33 | void |
| 34 | use(const T &p) |
| 35 | { |
| 36 | placeholder(&p); |
| 37 | } |
| 38 | |
| 39 | struct deleter { void operator()(int*) {} }; |
| 40 | |
| 41 | std::shared_ptr<int> make(uintptr_t p) |
| 42 | { |
| 43 | return std::shared_ptr<int>(reinterpret_cast<int*>(p), deleter()); |
| 44 | } |
| 45 | |
| 46 | int |
| 47 | main() |
| 48 | { |
| 49 | typedef std::shared_ptr<int> shared; |
| 50 | typedef std::weak_ptr<int> weak; |
| 51 | |
| 52 | shared esp; |
| 53 | // { dg-final { note-test esp "std::shared_ptr<int> (empty) = {get() = 0x0}" } } |
| 54 | weak ewp1; |
| 55 | // { dg-final { note-test ewp1 "std::weak_ptr<int> (empty) = {get() = 0x0}" } } |
| 56 | weak ewp2 = esp; |
| 57 | // { dg-final { note-test ewp2 "std::weak_ptr<int> (empty) = {get() = 0x0}" } } |
| 58 | |
| 59 | shared sp1 = make(0x12345678); |
| 60 | shared sp2 = sp1; |
| 61 | // { dg-final { note-test sp1 "std::shared_ptr<int> (use count 2, weak count 0) = {get() = 0x12345678}" } } |
| 62 | |
| 63 | shared sp3 = make(0x12344321); |
| 64 | weak sp4 = sp3; |
| 65 | weak wp1 = sp3; |
| 66 | // { dg-final { note-test wp1 "std::weak_ptr<int> (use count 1, weak count 2) = {get() = 0x12344321}" } } |
| 67 | |
| 68 | shared sp5 = make(0x56788765); |
| 69 | weak wp2 = sp5; |
| 70 | sp5.reset(); |
| 71 | // { dg-final { note-test wp2 "std::weak_ptr<int> (expired, weak count 1) = {get() = 0x56788765}" } } |
| 72 | |
| 73 | placeholder(""); // Mark SPOT |
| 74 | use(esp); |
| 75 | use(ewp1); |
| 76 | use(ewp2); |
| 77 | use(sp1); |
| 78 | use(wp1); |
| 79 | use(wp2); |
| 80 | |
| 81 | std::cout << "\n"; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | // { dg-final { gdb-test SPOT } } |