]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/function/8.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / function / 8.cc
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
3
4 // 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
5 //
6 // Copyright (C) 2005-2023 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
23 // 20.7.15 polymorphic function object wrapper
24 #include <functional>
25 #include <testsuite_hooks.h>
26 #include <testsuite_tr1.h>
27
28 template<typename T>
29 const T&
30 as_const(T& t)
31 { return t; }
32
33 // Check that f's target is a reference_wrapper bound to obj.
34 template<typename Function, typename T>
35 bool
36 wraps(Function& f, T& obj)
37 {
38 using ref_wrapper_type = std::reference_wrapper<T>;
39 auto* p = f.template target<ref_wrapper_type>();
40 return std::addressof(p->get()) == std::addressof(obj);
41 }
42
43 // Put reference_wrappers to member pointers
44 void test08()
45 {
46 using std::function;
47 using std::ref;
48 using std::cref;
49 using std::reference_wrapper;
50 using __gnu_test::X;
51
52 int X::* X_bar = &X::bar;
53 int (X::* X_foo)() = &X::foo;
54 int (X::* X_foo_c)() const = &X::foo_c;
55 int (X::* X_foo_v)() volatile = &X::foo_v;
56 int (X::* X_foo_cv)() const volatile = &X::foo_cv;
57
58 X x;
59 x.bar = 17;
60
61 function<int(X&)> frm(ref(X_bar));
62 VERIFY( frm );
63 VERIFY( frm(x) == 17 );
64 #if __cpp_rtti
65 VERIFY( typeid(ref(X_bar)) == frm.target_type() );
66 #endif
67 VERIFY( wraps(frm, X_bar) );
68
69 function<int(X&)> fr(ref(X_foo));
70 VERIFY( fr );
71 VERIFY( fr(x) == 1 );
72 #if __cpp_rtti
73 VERIFY( typeid(ref(X_foo)) == fr.target_type() );
74 #endif
75 VERIFY( wraps(fr, X_foo) );
76
77 function<int(const X&)> frc(ref(X_foo_c));
78 VERIFY( frc );
79 VERIFY( frc(x) == 2 );
80 #if __cpp_rtti
81 VERIFY( typeid(ref(X_foo_c)) == frc.target_type() );
82 #endif
83 VERIFY( wraps(frc, X_foo_c) );
84
85 function<int(volatile X&)> frv(ref(X_foo_v));
86 VERIFY( frv );
87 VERIFY( frv(x) == 3 );
88 #if __cpp_rtti
89 VERIFY( typeid(ref(X_foo_v)) == frv.target_type() );
90 #endif
91 VERIFY( wraps(frv, X_foo_v) );
92
93 function<int(const volatile X&)> frcv(ref(X_foo_cv));
94 VERIFY( frcv );
95 VERIFY( frcv(x) == 4 );
96 #if __cpp_rtti
97 VERIFY( typeid(ref(X_foo_cv)) == frcv.target_type() );
98 #endif
99 VERIFY( wraps(frcv, X_foo_cv) );
100
101 function<int(X*)> grm(ref(X_bar));
102 VERIFY( grm );
103 VERIFY( grm(&x) == 17 );
104 #if __cpp_rtti
105 VERIFY( typeid(ref(X_bar)) == grm.target_type() );
106 #endif
107 VERIFY( wraps(grm, X_bar) );
108
109 function<int(X*)> gr(ref(X_foo));
110 VERIFY( gr );
111 VERIFY( gr(&x) == 1 );
112 #if __cpp_rtti
113 VERIFY( typeid(ref(X_foo)) == gr.target_type() );
114 #endif
115 VERIFY( wraps(gr, X_foo) );
116
117 function<int(const X*)> grc(ref(X_foo_c));
118 VERIFY( grc );
119 VERIFY( grc(&x) == 2 );
120 #if __cpp_rtti
121 VERIFY( typeid(ref(X_foo_c)) == grc.target_type() );
122 #endif
123 VERIFY( wraps(grc, X_foo_c) );
124
125 function<int(volatile X*)> grv(ref(X_foo_v));
126 VERIFY( grv );
127 VERIFY( grv(&x) == 3 );
128 #if __cpp_rtti
129 VERIFY( typeid(ref(X_foo_v)) == grv.target_type() );
130 #endif
131 VERIFY( wraps(grv, X_foo_v) );
132
133 function<int(const volatile X*)> grcv(ref(X_foo_cv));
134 VERIFY( grcv );
135 VERIFY( grcv(&x) == 4 );
136 #if __cpp_rtti
137 VERIFY( typeid(ref(X_foo_cv)) == grcv.target_type() );
138 #endif
139 VERIFY( wraps(grcv, X_foo_cv) );
140
141 function<int(X&)> hrm(cref(X_bar));
142 VERIFY( hrm );
143 VERIFY( hrm(x) == 17 );
144 #if __cpp_rtti
145 VERIFY( typeid(cref(X_bar)) == hrm.target_type() );
146 #endif
147 VERIFY( wraps(hrm, as_const(X_bar)) );
148
149 function<int(X&)> hr(cref(X_foo));
150 VERIFY( hr );
151 VERIFY( hr(x) == 1 );
152 #if __cpp_rtti
153 VERIFY( typeid(cref(X_foo)) == hr.target_type() );
154 #endif
155 VERIFY( wraps(hr, as_const(X_foo)) );
156
157 function<int(const X&)> hrc(cref(X_foo_c));
158 VERIFY( hrc );
159 VERIFY( hrc(x) == 2 );
160 #if __cpp_rtti
161 VERIFY( typeid(cref(X_foo_c)) == hrc.target_type() );
162 #endif
163 VERIFY( wraps(hrc, as_const(X_foo_c)) );
164
165 function<int(volatile X&)> hrv(cref(X_foo_v));
166 VERIFY( hrv );
167 VERIFY( hrv(x) == 3 );
168 #if __cpp_rtti
169 VERIFY( typeid(cref(X_foo_v)) == hrv.target_type() );
170 #endif
171 VERIFY( wraps(hrv, as_const(X_foo_v)) );
172
173 function<int(const volatile X&)> hrcv(cref(X_foo_cv));
174 VERIFY( hrcv );
175 VERIFY( hrcv(x) == 4 );
176 #if __cpp_rtti
177 VERIFY( typeid(cref(X_foo_cv)) == hrcv.target_type() );
178 #endif
179 VERIFY( wraps(hrcv, as_const(X_foo_cv)) );
180 }
181
182 int main()
183 {
184 test08();
185 return 0;
186 }