Debian Codegraph @

libstdc++-v3/testsuite/20_util/function/5.cc

raw
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
3
4 // 2005-01-15 Douglas Gregor <[email protected]>
5 //
6 // Copyright (C) 2005-2025 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 using namespace __gnu_test;
29
30 // Put member pointers into function<> wrappers
31 void test05()
32 {
33 using std::function;
34
35 X x;
36 x.bar = 17;
37
38 function<int(X&)> frm(&X::bar);
39 VERIFY( frm );
40 VERIFY( frm(x) == 17 );
41 #if __cpp_rtti
42 VERIFY( typeid(int X::*) == frm.target_type() );
43 #endif
44 VERIFY( *frm.target<int X::*>() == &X::bar );
45
46 function<int(X&)> fr(&X::foo);
47 VERIFY( fr );
48 VERIFY( fr(x) == 1 );
49 #if __cpp_rtti
50 VERIFY( typeid(int (X::*)()) == fr.target_type() );
51 #endif
52 VERIFY( *fr.target<int (X::*)()>() == &X::foo );
53
54 function<int(const X&)> frc(&X::foo_c);
55 VERIFY( frc );
56 VERIFY( frc(x) == 2 );
57 #if __cpp_rtti
58 VERIFY( typeid(int (X::*)() const) == frc.target_type() );
59 #endif
60 VERIFY( *frc.target<int (X::*)() const >() == &X::foo_c );
61
62 function<int(volatile X&)> frv(&X::foo_v);
63 VERIFY( frv );
64 VERIFY( frv(x) == 3 );
65 #if __cpp_rtti
66 VERIFY( typeid(int (X::*)() volatile) == frv.target_type() );
67 #endif
68 VERIFY( *frv.target<int (X::*)() volatile >() == &X::foo_v );
69 VERIFY( frv.target<int (X::*)() const volatile>() == 0 );
70
71 function<int(const volatile X&)> frcv(&X::foo_cv);
72 VERIFY( frcv );
73 VERIFY( frcv(x) == 4 );
74 #if __cpp_rtti
75 VERIFY( typeid(int (X::*)() const volatile) == frcv.target_type() );
76 #endif
77 VERIFY( *frcv.target<int (X::*)() const volatile >() == &X::foo_cv );
78 VERIFY( frcv.target<int (X::*)() const>() == 0 );
79
80 function<int(X*)> grm(&X::bar);
81 VERIFY( grm );
82 VERIFY( grm(&x) == 17 );
83 #if __cpp_rtti
84 VERIFY( typeid(int X::*) == grm.target_type() );
85 #endif
86 VERIFY( *grm.target<int X::*>() == &X::bar );
87
88 function<int(X*)> gr(&X::foo);
89 VERIFY( gr );
90 VERIFY( gr(&x) == 1 );
91 #if __cpp_rtti
92 VERIFY( typeid(int (X::*)()) == gr.target_type() );
93 #endif
94 VERIFY( *gr.target<int (X::*)()>() == &X::foo );
95
96 function<int(const X*)> grc(&X::foo_c);
97 VERIFY( grc );
98 VERIFY( grc(&x) == 2 );
99 #if __cpp_rtti
100 VERIFY( typeid(int (X::*)() const) == grc.target_type() );
101 #endif
102 VERIFY( *grc.target<int (X::*)() const >() == &X::foo_c );
103
104 function<int(volatile X*)> grv(&X::foo_v);
105 VERIFY( grv );
106 VERIFY( grv(&x) == 3 );
107 #if __cpp_rtti
108 VERIFY( typeid(int (X::*)() volatile) == grv.target_type() );
109 #endif
110 VERIFY( *grv.target<int (X::*)() volatile >() == &X::foo_v );
111 VERIFY( grv.target<int (X::*)() const volatile>() == 0 );
112
113 function<int(const volatile X*)> grcv(&X::foo_cv);
114 VERIFY( grcv );
115 VERIFY( grcv(&x) == 4 );
116 #if __cpp_rtti
117 VERIFY( typeid(int (X::*)() const volatile) == grcv.target_type() );
118 #endif
119 VERIFY( *grcv.target<int (X::*)() const volatile >() == &X::foo_cv );
120 VERIFY( grcv.target<int (X::*)() const>() == 0 );
121 }
122
123 int main()
124 {
125 test05();
126 return 0;
127 }