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