| 1 | #include <boost/config.hpp> |
| 2 | |
| 3 | #if defined(BOOST_MSVC) |
| 4 | #pragma warning(disable: 4786) // identifier truncated in debug info |
| 5 | #pragma warning(disable: 4710) // function not inlined |
| 6 | #pragma warning(disable: 4711) // function selected for automatic inline expansion |
| 7 | #pragma warning(disable: 4514) // unreferenced inline removed |
| 8 | #endif |
| 9 | |
| 10 | // |
| 11 | // mem_fn_unary_addr_test.cpp - poisoned operator& test |
| 12 | // |
| 13 | // Copyright (c) 2009 Peter Dimov |
| 14 | // |
| 15 | // Distributed under the Boost Software License, Version 1.0. |
| 16 | // See accompanying file LICENSE_1_0.txt or copy at |
| 17 | // http://www.boost.org/LICENSE_1_0.txt |
| 18 | // |
| 19 | |
| 20 | #include <boost/mem_fn.hpp> |
| 21 | #include <iostream> |
| 22 | |
| 23 | unsigned int hash = 0; |
| 24 | |
| 25 | struct X |
| 26 | { |
| 27 | int f0() { f1(a1: 17); return 0; } |
| 28 | int g0() const { g1(a1: 17); return 0; } |
| 29 | |
| 30 | int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; } |
| 31 | int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; } |
| 32 | |
| 33 | int f2(int a1, int a2) { f1(a1); f1(a1: a2); return 0; } |
| 34 | int g2(int a1, int a2) const { g1(a1); g1(a1: a2); return 0; } |
| 35 | |
| 36 | int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a1: a3); return 0; } |
| 37 | int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a1: a3); return 0; } |
| 38 | |
| 39 | int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a1: a4); return 0; } |
| 40 | int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a1: a4); return 0; } |
| 41 | |
| 42 | int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a1: a5); return 0; } |
| 43 | int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a1: a5); return 0; } |
| 44 | |
| 45 | int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a1: a6); return 0; } |
| 46 | int g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a1: a6); return 0; } |
| 47 | |
| 48 | int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a1: a7); return 0; } |
| 49 | int g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a1: a7); return 0; } |
| 50 | |
| 51 | int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a1: a8); return 0; } |
| 52 | int g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a1: a8); return 0; } |
| 53 | }; |
| 54 | |
| 55 | template<class T> class Y |
| 56 | { |
| 57 | private: |
| 58 | |
| 59 | T * pt_; |
| 60 | |
| 61 | void operator& (); |
| 62 | void operator& () const; |
| 63 | |
| 64 | public: |
| 65 | |
| 66 | explicit Y( T * pt ): pt_( pt ) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | T * get() const |
| 71 | { |
| 72 | return pt_; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | template<class T> T * get_pointer( Y< T > const & y ) |
| 77 | { |
| 78 | return y.get(); |
| 79 | } |
| 80 | |
| 81 | int detect_errors(bool x) |
| 82 | { |
| 83 | if( x ) |
| 84 | { |
| 85 | std::cerr << "no errors detected.\n" ; |
| 86 | return 0; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | std::cerr << "test failed.\n" ; |
| 91 | return 1; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | int main() |
| 96 | { |
| 97 | using boost::mem_fn; |
| 98 | |
| 99 | X x; |
| 100 | |
| 101 | Y<X> px( &x ); |
| 102 | Y<X const> pcx( &x ); |
| 103 | |
| 104 | mem_fn(pmf: &X::f0)( px ); |
| 105 | mem_fn(pmf: &X::g0)( pcx ); |
| 106 | |
| 107 | mem_fn(pmf: &X::f1)( px, 1 ); |
| 108 | mem_fn(pmf: &X::g1)( pcx, 1 ); |
| 109 | |
| 110 | mem_fn(pmf: &X::f2)( px, 1, 2 ); |
| 111 | mem_fn(pmf: &X::g2)( pcx, 1, 2 ); |
| 112 | |
| 113 | mem_fn(pmf: &X::f3)( px, 1, 2, 3 ); |
| 114 | mem_fn(pmf: &X::g3)( pcx, 1, 2, 3 ); |
| 115 | |
| 116 | mem_fn(pmf: &X::f4)( px, 1, 2, 3, 4 ); |
| 117 | mem_fn(pmf: &X::g4)( pcx, 1, 2, 3, 4 ); |
| 118 | |
| 119 | mem_fn(pmf: &X::f5)( px, 1, 2, 3, 4, 5 ); |
| 120 | mem_fn(pmf: &X::g5)( pcx, 1, 2, 3, 4, 5 ); |
| 121 | |
| 122 | mem_fn(pmf: &X::f6)( px, 1, 2, 3, 4, 5, 6 ); |
| 123 | mem_fn(pmf: &X::g6)( pcx, 1, 2, 3, 4, 5, 6 ); |
| 124 | |
| 125 | mem_fn(pmf: &X::f7)( px, 1, 2, 3, 4, 5, 6, 7 ); |
| 126 | mem_fn(pmf: &X::g7)( pcx, 1, 2, 3, 4, 5, 6, 7 ); |
| 127 | |
| 128 | mem_fn(pmf: &X::f8)( px, 1, 2, 3, 4, 5, 6, 7, 8 ); |
| 129 | mem_fn(pmf: &X::g8)( pcx, 1, 2, 3, 4, 5, 6, 7, 8 ); |
| 130 | |
| 131 | return detect_errors( x: hash == 2155 ); |
| 132 | } |
| 133 | |