]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/function/8.cc
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
4 // 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
6 // Copyright (C) 2005-2024 Free Software Foundation, Inc.
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)
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.
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/>.
23 // 20.7.15 polymorphic function object wrapper
25 #include <testsuite_hooks.h>
26 #include <testsuite_tr1.h>
33 // Check that f's target is a reference_wrapper bound to obj.
34 template<typename Function
, typename T
>
36 wraps(Function
& f
, T
& obj
)
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
);
43 // Put reference_wrappers to member pointers
49 using std::reference_wrapper
;
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
;
61 function
<int(X
&)> frm(ref(X_bar
));
63 VERIFY( frm(x
) == 17 );
65 VERIFY( typeid(ref(X_bar
)) == frm
.target_type() );
67 VERIFY( wraps(frm
, X_bar
) );
69 function
<int(X
&)> fr(ref(X_foo
));
73 VERIFY( typeid(ref(X_foo
)) == fr
.target_type() );
75 VERIFY( wraps(fr
, X_foo
) );
77 function
<int(const X
&)> frc(ref(X_foo_c
));
79 VERIFY( frc(x
) == 2 );
81 VERIFY( typeid(ref(X_foo_c
)) == frc
.target_type() );
83 VERIFY( wraps(frc
, X_foo_c
) );
85 function
<int(volatile X
&)> frv(ref(X_foo_v
));
87 VERIFY( frv(x
) == 3 );
89 VERIFY( typeid(ref(X_foo_v
)) == frv
.target_type() );
91 VERIFY( wraps(frv
, X_foo_v
) );
93 function
<int(const volatile X
&)> frcv(ref(X_foo_cv
));
95 VERIFY( frcv(x
) == 4 );
97 VERIFY( typeid(ref(X_foo_cv
)) == frcv
.target_type() );
99 VERIFY( wraps(frcv
, X_foo_cv
) );
101 function
<int(X
*)> grm(ref(X_bar
));
103 VERIFY( grm(&x
) == 17 );
105 VERIFY( typeid(ref(X_bar
)) == grm
.target_type() );
107 VERIFY( wraps(grm
, X_bar
) );
109 function
<int(X
*)> gr(ref(X_foo
));
111 VERIFY( gr(&x
) == 1 );
113 VERIFY( typeid(ref(X_foo
)) == gr
.target_type() );
115 VERIFY( wraps(gr
, X_foo
) );
117 function
<int(const X
*)> grc(ref(X_foo_c
));
119 VERIFY( grc(&x
) == 2 );
121 VERIFY( typeid(ref(X_foo_c
)) == grc
.target_type() );
123 VERIFY( wraps(grc
, X_foo_c
) );
125 function
<int(volatile X
*)> grv(ref(X_foo_v
));
127 VERIFY( grv(&x
) == 3 );
129 VERIFY( typeid(ref(X_foo_v
)) == grv
.target_type() );
131 VERIFY( wraps(grv
, X_foo_v
) );
133 function
<int(const volatile X
*)> grcv(ref(X_foo_cv
));
135 VERIFY( grcv(&x
) == 4 );
137 VERIFY( typeid(ref(X_foo_cv
)) == grcv
.target_type() );
139 VERIFY( wraps(grcv
, X_foo_cv
) );
141 function
<int(X
&)> hrm(cref(X_bar
));
143 VERIFY( hrm(x
) == 17 );
145 VERIFY( typeid(cref(X_bar
)) == hrm
.target_type() );
147 VERIFY( wraps(hrm
, as_const(X_bar
)) );
149 function
<int(X
&)> hr(cref(X_foo
));
151 VERIFY( hr(x
) == 1 );
153 VERIFY( typeid(cref(X_foo
)) == hr
.target_type() );
155 VERIFY( wraps(hr
, as_const(X_foo
)) );
157 function
<int(const X
&)> hrc(cref(X_foo_c
));
159 VERIFY( hrc(x
) == 2 );
161 VERIFY( typeid(cref(X_foo_c
)) == hrc
.target_type() );
163 VERIFY( wraps(hrc
, as_const(X_foo_c
)) );
165 function
<int(volatile X
&)> hrv(cref(X_foo_v
));
167 VERIFY( hrv(x
) == 3 );
169 VERIFY( typeid(cref(X_foo_v
)) == hrv
.target_type() );
171 VERIFY( wraps(hrv
, as_const(X_foo_v
)) );
173 function
<int(const volatile X
&)> hrcv(cref(X_foo_cv
));
175 VERIFY( hrcv(x
) == 4 );
177 VERIFY( typeid(cref(X_foo_cv
)) == hrcv
.target_type() );
179 VERIFY( wraps(hrcv
, as_const(X_foo_cv
)) );