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