]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/to_address/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / to_address / 1.cc
CommitLineData
99dee823 1// Copyright (C) 2017-2021 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
24class P1
25{
26public:
27 using element_type = int;
28
29 explicit P1(int* p)
30 : p_(p) { }
31
32 int* operator->() const noexcept
33 { return p_; }
34
35private:
36 int* p_;
37};
38
39class P2
40{
41public:
42 using element_type = int;
43
44 explicit P2(int* p)
45 : p_(p) { }
46
47 P1 operator->() const noexcept
48 { return p_; }
49
50private:
51 P1 p_;
52};
53
54class P3
55{
56public:
57 explicit P3(int* p)
58 : p_(p) { }
59
60 int* get() const noexcept
61 { return p_; }
62
63private:
64 int* p_;
65};
66
67namespace 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
77class P4
78{
79public:
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
89private:
90 int* p_;
91};
92
93namespace 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
103void test01()
104{
105 int i = 0;
106 int* p = &i;
107 VERIFY( std::to_address(p) == &i );
108}
109
110void test02()
111{
112 int i = 0;
113 P1 p(&i);
114 VERIFY( std::to_address(p) == &i );
115}
116
117void test03()
118{
119 int i = 0;
120 P2 p(&i);
121 VERIFY( std::to_address(p) == &i );
122}
123
124void test04()
125{
126 int i = 0;
127 P3 p(&i);
128 VERIFY( std::to_address(p) == &i );
129}
130
131void test05()
132{
133 int i = 0;
134 P4 p(&i);
135 VERIFY( std::to_address(p) == &i );
136}
137
138int main()
139{
140 test01();
141 test02();
142 test03();
143 test04();
144 test05();
145 return 0;
146}