| 1 | // { dg-do run { target c++14 } } |
| 2 | |
| 3 | // Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 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 | #include <experimental/memory> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | using std::experimental::observer_ptr; |
| 24 | |
| 25 | struct B {}; |
| 26 | struct D : B {}; |
| 27 | |
| 28 | void test01() |
| 29 | { |
| 30 | observer_ptr<int> a, b; |
| 31 | a = b; |
| 32 | VERIFY(a == b); |
| 33 | } |
| 34 | |
| 35 | void test02() |
| 36 | { |
| 37 | int x{}; |
| 38 | observer_ptr<int> a; |
| 39 | observer_ptr<int> b{&x}; |
| 40 | VERIFY(a != b); |
| 41 | a = b; |
| 42 | VERIFY(a == b); |
| 43 | } |
| 44 | |
| 45 | void test03() |
| 46 | { |
| 47 | int x{}; |
| 48 | observer_ptr<const int> a; |
| 49 | observer_ptr<int> b{&x}; |
| 50 | VERIFY(a != b); |
| 51 | a = b; |
| 52 | VERIFY(a == b); |
| 53 | } |
| 54 | |
| 55 | void test04() |
| 56 | { |
| 57 | D x{}; |
| 58 | observer_ptr<B> a; |
| 59 | observer_ptr<D> b{&x}; |
| 60 | VERIFY(a != b); |
| 61 | a = b; |
| 62 | VERIFY(a == b); |
| 63 | } |
| 64 | |
| 65 | constexpr bool test05_helper(observer_ptr<const int> a, |
| 66 | observer_ptr<const int> b) |
| 67 | { |
| 68 | a = b; |
| 69 | return (a.get() == b.get()); |
| 70 | } |
| 71 | |
| 72 | void test05() |
| 73 | { |
| 74 | static constexpr int x{}; |
| 75 | constexpr observer_ptr<const int> a; |
| 76 | constexpr observer_ptr<const int> b{&x}; |
| 77 | constexpr bool assigned = test05_helper(a, b); |
| 78 | VERIFY(assigned); |
| 79 | } |
| 80 | |
| 81 | int main() |
| 82 | { |
| 83 | test01(); |
| 84 | test02(); |
| 85 | test03(); |
| 86 | test04(); |
| 87 | test05(); |
| 88 | } |