| 1 | // 2005-01-26 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.5 function template mem_fn |
| 21 | #include <tr1/functional> |
| 22 | #include <testsuite_hooks.h> |
| 23 | #include <testsuite_tr1.h> |
| 24 | |
| 25 | struct X { int bar; }; |
| 26 | |
| 27 | struct Y : X { }; |
| 28 | |
| 29 | template<typename T> |
| 30 | struct dumb_ptr |
| 31 | { |
| 32 | dumb_ptr(T* p) : p(p) {} |
| 33 | |
| 34 | T& operator*() const { return *p; } |
| 35 | |
| 36 | private: |
| 37 | T* p; |
| 38 | }; |
| 39 | |
| 40 | // Test mem_fn with a data member |
| 41 | void test01(int r = 0) |
| 42 | { |
| 43 | using std::tr1::mem_fn; |
| 44 | |
| 45 | X x; |
| 46 | Y y; |
| 47 | const X& xc = x; |
| 48 | const Y& yc = y; |
| 49 | X* xp = &x; |
| 50 | Y* yp =&y; |
| 51 | const X* xpc = xp; |
| 52 | const Y* ypc = yp; |
| 53 | dumb_ptr<X> xd(xp); |
| 54 | dumb_ptr<Y> yd(yp); |
| 55 | const dumb_ptr<X>& xdc = xd; |
| 56 | const dumb_ptr<Y>& ydc = yd; |
| 57 | |
| 58 | int& bx = mem_fn(&X::bar)(x); |
| 59 | const int& bxc = mem_fn(&X::bar)(xc); |
| 60 | int& bxp = mem_fn(&X::bar)(xp); |
| 61 | const int& bxpc = mem_fn(&X::bar)(xpc); |
| 62 | const int& bxd = mem_fn(&X::bar)(xd); |
| 63 | const int& bxdc = mem_fn(&X::bar)(xdc); |
| 64 | |
| 65 | int& by = mem_fn(&X::bar)(y); |
| 66 | const int& byc = mem_fn(&X::bar)(yc); |
| 67 | int& byp = mem_fn(&X::bar)(yp); |
| 68 | const int& bypc = mem_fn(&X::bar)(ypc); |
| 69 | const int& byd = mem_fn(&X::bar)(yd); |
| 70 | const int& bydc = mem_fn(&X::bar)(ydc); |
| 71 | |
| 72 | // Avoid unused variable warnings. |
| 73 | r = bx + bxc + bxp + bxpc + bxd + bxdc + by + byc + byp + bypc + byd + bydc; |
| 74 | } |
| 75 | |
| 76 | int main() |
| 77 | { |
| 78 | test01(); |
| 79 | return 0; |
| 80 | } |