Merge from mainline
[official-gcc.git] / libstdc++-v3 / testsuite / tr1 / 3_function_objects / mem_fn.cc
blob2f780c1a994482ddc70ecfec76d8ab8eab5424d9
1 // 2005-01-26 Douglas Gregor <dgregor@cs.indiana.edu>
2 //
3 // Copyright (C) 2005 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 2, or (at your option)
9 // any later version.
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // 3.5 function template mem_fn
22 #include <tr1/functional>
23 #include <testsuite_hooks.h>
24 #include <testsuite_tr1.h>
26 struct X { int bar; };
28 struct Y : X { };
30 template<typename T>
31 struct dumb_ptr
33 dumb_ptr(T* p) : p(p) {}
35 T& operator*() const { return *p; }
37 private:
38 T* p;
41 // Test mem_fn with a data member
42 void test01(int r = 0)
44 using std::tr1::mem_fn;
46 X x;
47 Y y;
48 const X& xc = x;
49 const Y& yc = y;
50 X* xp = &x;
51 Y* yp =&y;
52 const X* xpc = xp;
53 const Y* ypc = yp;
54 dumb_ptr<X> xd(xp);
55 dumb_ptr<Y> yd(yp);
56 const dumb_ptr<X>& xdc = xd;
57 const dumb_ptr<Y>& ydc = yd;
59 int& bx = mem_fn(&X::bar)(x);
60 const int& bxc = mem_fn(&X::bar)(xc);
61 int& bxp = mem_fn(&X::bar)(xp);
62 const int& bxpc = mem_fn(&X::bar)(xpc);
63 const int& bxd = mem_fn(&X::bar)(xd);
64 const int& bxdc = mem_fn(&X::bar)(xdc);
66 int& by = mem_fn(&X::bar)(y);
67 const int& byc = mem_fn(&X::bar)(yc);
68 int& byp = mem_fn(&X::bar)(yp);
69 const int& bypc = mem_fn(&X::bar)(ypc);
70 const int& byd = mem_fn(&X::bar)(yd);
71 const int& bydc = mem_fn(&X::bar)(ydc);
73 // Avoid unused variable warnings.
74 r = bx + bxc + bxp + bxpc + bxd + bxdc + by + byc + byp + bypc + byd + bydc;
77 int main()
79 test01();
80 return 0;