| 1 | // Copyright (C) 2017-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++20 } } |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | class P1 |
| 24 | { |
| 25 | public: |
| 26 | using element_type = int; |
| 27 | |
| 28 | explicit P1(int* p) |
| 29 | : p_(p) { } |
| 30 | |
| 31 | int* operator->() const noexcept |
| 32 | { return p_; } |
| 33 | |
| 34 | private: |
| 35 | int* p_; |
| 36 | }; |
| 37 | |
| 38 | class P2 |
| 39 | { |
| 40 | public: |
| 41 | using element_type = int; |
| 42 | |
| 43 | explicit P2(int* p) |
| 44 | : p_(p) { } |
| 45 | |
| 46 | P1 operator->() const noexcept |
| 47 | { return p_; } |
| 48 | |
| 49 | private: |
| 50 | P1 p_; |
| 51 | }; |
| 52 | |
| 53 | class P3 |
| 54 | { |
| 55 | public: |
| 56 | explicit P3(int* p) |
| 57 | : p_(p) { } |
| 58 | |
| 59 | int* get() const noexcept |
| 60 | { return p_; } |
| 61 | |
| 62 | private: |
| 63 | int* p_; |
| 64 | }; |
| 65 | |
| 66 | namespace std |
| 67 | { |
| 68 | template<> |
| 69 | struct pointer_traits<::P3> |
| 70 | { |
| 71 | static int* to_address(const ::P3& p) noexcept |
| 72 | { return p.get(); } |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | class P4 |
| 77 | { |
| 78 | public: |
| 79 | explicit P4(int* p) |
| 80 | : p_(p) { } |
| 81 | |
| 82 | int* operator->() const noexcept |
| 83 | { return nullptr; } |
| 84 | |
| 85 | int* get() const noexcept |
| 86 | { return p_; } |
| 87 | |
| 88 | private: |
| 89 | int* p_; |
| 90 | }; |
| 91 | |
| 92 | namespace std |
| 93 | { |
| 94 | template<> |
| 95 | struct pointer_traits<::P4> |
| 96 | { |
| 97 | static int* to_address(const ::P4& p) noexcept |
| 98 | { return p.get(); } |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | void test01() |
| 103 | { |
| 104 | int i = 0; |
| 105 | int* p = &i; |
| 106 | VERIFY( std::to_address(p) == &i ); |
| 107 | } |
| 108 | |
| 109 | void test02() |
| 110 | { |
| 111 | int i = 0; |
| 112 | P1 p(&i); |
| 113 | VERIFY( std::to_address(p) == &i ); |
| 114 | } |
| 115 | |
| 116 | void test03() |
| 117 | { |
| 118 | int i = 0; |
| 119 | P2 p(&i); |
| 120 | VERIFY( std::to_address(p) == &i ); |
| 121 | } |
| 122 | |
| 123 | void test04() |
| 124 | { |
| 125 | int i = 0; |
| 126 | P3 p(&i); |
| 127 | VERIFY( std::to_address(p) == &i ); |
| 128 | } |
| 129 | |
| 130 | void test05() |
| 131 | { |
| 132 | int i = 0; |
| 133 | P4 p(&i); |
| 134 | VERIFY( std::to_address(p) == &i ); |
| 135 | } |
| 136 | |
| 137 | int main() |
| 138 | { |
| 139 | test01(); |
| 140 | test02(); |
| 141 | test03(); |
| 142 | test04(); |
| 143 | test05(); |
| 144 | return 0; |
| 145 | } |