]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/function/8.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / function / 8.cc
CommitLineData
52066eae 1// { dg-do run { target c++11 } }
7cc9022f
AA
2// { dg-require-effective-target hosted }
3
7aec2c63
PC
4// 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
5//
a945c346 6// Copyright (C) 2005-2024 Free Software Foundation, Inc.
7aec2c63
PC
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
4704f28e
JW
28template<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.
34template<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 }
7aec2c63 42
7aec2c63
PC
43// Put reference_wrappers to member pointers
44void test08()
45{
46 using std::function;
47 using std::ref;
48 using std::cref;
4704f28e
JW
49 using std::reference_wrapper;
50 using __gnu_test::X;
7aec2c63
PC
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 );
b6b66006 64#if __cpp_rtti
4704f28e 65 VERIFY( typeid(ref(X_bar)) == frm.target_type() );
b6b66006 66#endif
4704f28e 67 VERIFY( wraps(frm, X_bar) );
7aec2c63
PC
68
69 function<int(X&)> fr(ref(X_foo));
70 VERIFY( fr );
71 VERIFY( fr(x) == 1 );
b6b66006 72#if __cpp_rtti
4704f28e 73 VERIFY( typeid(ref(X_foo)) == fr.target_type() );
b6b66006 74#endif
4704f28e 75 VERIFY( wraps(fr, X_foo) );
7aec2c63
PC
76
77 function<int(const X&)> frc(ref(X_foo_c));
78 VERIFY( frc );
79 VERIFY( frc(x) == 2 );
b6b66006 80#if __cpp_rtti
4704f28e 81 VERIFY( typeid(ref(X_foo_c)) == frc.target_type() );
b6b66006 82#endif
4704f28e 83 VERIFY( wraps(frc, X_foo_c) );
7aec2c63
PC
84
85 function<int(volatile X&)> frv(ref(X_foo_v));
86 VERIFY( frv );
87 VERIFY( frv(x) == 3 );
b6b66006 88#if __cpp_rtti
4704f28e 89 VERIFY( typeid(ref(X_foo_v)) == frv.target_type() );
b6b66006 90#endif
4704f28e 91 VERIFY( wraps(frv, X_foo_v) );
7aec2c63
PC
92
93 function<int(const volatile X&)> frcv(ref(X_foo_cv));
94 VERIFY( frcv );
95 VERIFY( frcv(x) == 4 );
b6b66006 96#if __cpp_rtti
4704f28e 97 VERIFY( typeid(ref(X_foo_cv)) == frcv.target_type() );
b6b66006 98#endif
4704f28e 99 VERIFY( wraps(frcv, X_foo_cv) );
7aec2c63
PC
100
101 function<int(X*)> grm(ref(X_bar));
102 VERIFY( grm );
103 VERIFY( grm(&x) == 17 );
b6b66006 104#if __cpp_rtti
4704f28e 105 VERIFY( typeid(ref(X_bar)) == grm.target_type() );
b6b66006 106#endif
4704f28e 107 VERIFY( wraps(grm, X_bar) );
7aec2c63
PC
108
109 function<int(X*)> gr(ref(X_foo));
110 VERIFY( gr );
111 VERIFY( gr(&x) == 1 );
b6b66006 112#if __cpp_rtti
4704f28e 113 VERIFY( typeid(ref(X_foo)) == gr.target_type() );
b6b66006 114#endif
4704f28e 115 VERIFY( wraps(gr, X_foo) );
7aec2c63
PC
116
117 function<int(const X*)> grc(ref(X_foo_c));
118 VERIFY( grc );
119 VERIFY( grc(&x) == 2 );
b6b66006 120#if __cpp_rtti
4704f28e 121 VERIFY( typeid(ref(X_foo_c)) == grc.target_type() );
b6b66006 122#endif
4704f28e 123 VERIFY( wraps(grc, X_foo_c) );
7aec2c63
PC
124
125 function<int(volatile X*)> grv(ref(X_foo_v));
126 VERIFY( grv );
127 VERIFY( grv(&x) == 3 );
b6b66006 128#if __cpp_rtti
4704f28e 129 VERIFY( typeid(ref(X_foo_v)) == grv.target_type() );
b6b66006 130#endif
4704f28e 131 VERIFY( wraps(grv, X_foo_v) );
7aec2c63
PC
132
133 function<int(const volatile X*)> grcv(ref(X_foo_cv));
134 VERIFY( grcv );
135 VERIFY( grcv(&x) == 4 );
b6b66006 136#if __cpp_rtti
4704f28e 137 VERIFY( typeid(ref(X_foo_cv)) == grcv.target_type() );
b6b66006 138#endif
4704f28e 139 VERIFY( wraps(grcv, X_foo_cv) );
7aec2c63
PC
140
141 function<int(X&)> hrm(cref(X_bar));
142 VERIFY( hrm );
143 VERIFY( hrm(x) == 17 );
b6b66006 144#if __cpp_rtti
4704f28e 145 VERIFY( typeid(cref(X_bar)) == hrm.target_type() );
b6b66006 146#endif
4704f28e 147 VERIFY( wraps(hrm, as_const(X_bar)) );
7aec2c63
PC
148
149 function<int(X&)> hr(cref(X_foo));
150 VERIFY( hr );
151 VERIFY( hr(x) == 1 );
b6b66006 152#if __cpp_rtti
4704f28e 153 VERIFY( typeid(cref(X_foo)) == hr.target_type() );
b6b66006 154#endif
4704f28e 155 VERIFY( wraps(hr, as_const(X_foo)) );
7aec2c63
PC
156
157 function<int(const X&)> hrc(cref(X_foo_c));
158 VERIFY( hrc );
159 VERIFY( hrc(x) == 2 );
b6b66006 160#if __cpp_rtti
4704f28e 161 VERIFY( typeid(cref(X_foo_c)) == hrc.target_type() );
b6b66006 162#endif
4704f28e 163 VERIFY( wraps(hrc, as_const(X_foo_c)) );
7aec2c63
PC
164
165 function<int(volatile X&)> hrv(cref(X_foo_v));
166 VERIFY( hrv );
167 VERIFY( hrv(x) == 3 );
b6b66006 168#if __cpp_rtti
4704f28e 169 VERIFY( typeid(cref(X_foo_v)) == hrv.target_type() );
b6b66006 170#endif
4704f28e 171 VERIFY( wraps(hrv, as_const(X_foo_v)) );
7aec2c63
PC
172
173 function<int(const volatile X&)> hrcv(cref(X_foo_cv));
174 VERIFY( hrcv );
175 VERIFY( hrcv(x) == 4 );
b6b66006 176#if __cpp_rtti
4704f28e 177 VERIFY( typeid(cref(X_foo_cv)) == hrcv.target_type() );
b6b66006 178#endif
4704f28e 179 VERIFY( wraps(hrcv, as_const(X_foo_cv)) );
7aec2c63
PC
180}
181
182int main()
183{
184 test08();
185 return 0;
186}