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