1 #include <boost/config.hpp>
2 #include <boost/detail/workaround.hpp>
3
4 #if defined(BOOST_MSVC)
5 #pragma warning(disable: 4786) // identifier truncated in debug info
6 #pragma warning(disable: 4710) // function not inlined
7 #pragma warning(disable: 4711) // function selected for automatic inline expansion
8 #pragma warning(disable: 4514) // unreferenced inline removed
9 #endif
10
11 //
12 // mem_fn_unary_addr_test.cpp - poisoned operator& test
13 //
14 // Copyright (c) 2009 Peter Dimov
15 //
16 // Distributed under the Boost Software License, Version 1.0.
17 // See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt
19 //
20
21 #include <boost/mem_fn.hpp>
22
23 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
24 #pragma warning(push, 3)
25 #endif
26
27 #include <iostream>
28
29 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
30 #pragma warning(pop)
31 #endif
32
33 unsigned int hash = 0;
34
35 struct X
36 {
f0X37 int f0() { f1(17); return 0; }
g0X38 int g0() const { g1(17); return 0; }
39
f1X40 int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; }
g1X41 int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; }
42
f2X43 int f2(int a1, int a2) { f1(a1); f1(a2); return 0; }
g2X44 int g2(int a1, int a2) const { g1(a1); g1(a2); return 0; }
45
f3X46 int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; }
g3X47 int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; }
48
f4X49 int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; }
g4X50 int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; }
51
f5X52 int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; }
g5X53 int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; }
54
f6X55 int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; }
g6X56 int g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); return 0; }
57
f7X58 int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); return 0; }
g7X59 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(a7); return 0; }
60
f8X61 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(a8); return 0; }
g8X62 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(a8); return 0; }
63 };
64
65 template<class T> class Y
66 {
67 private:
68
69 T * pt_;
70
71 void operator& ();
72 void operator& () const;
73
74 public:
75
Y(T * pt)76 explicit Y( T * pt ): pt_( pt )
77 {
78 }
79
get() const80 T * get() const
81 {
82 return pt_;
83 }
84 };
85
86 #if defined( BOOST_BORLANDC ) && BOOST_WORKAROUND( BOOST_BORLANDC, BOOST_TESTED_AT( 0x620 ) )
87 namespace boost
88 {
89 #endif
90
get_pointer(Y<T> const & y)91 template<class T> T * get_pointer( Y< T > const & y )
92 {
93 return y.get();
94 }
95
96 #if defined( BOOST_BORLANDC ) && BOOST_WORKAROUND( BOOST_BORLANDC, BOOST_TESTED_AT( 0x620 ) )
97 } // namespace boost
98 #endif
99
detect_errors(bool x)100 int detect_errors(bool x)
101 {
102 if( x )
103 {
104 std::cerr << "no errors detected.\n";
105 return 0;
106 }
107 else
108 {
109 std::cerr << "test failed.\n";
110 return 1;
111 }
112 }
113
main()114 int main()
115 {
116 using boost::mem_fn;
117
118 X x;
119
120 Y<X> px( &x );
121 Y<X const> pcx( &x );
122
123 mem_fn(&X::f0)( px );
124 mem_fn(&X::g0)( pcx );
125
126 mem_fn(&X::f1)( px, 1 );
127 mem_fn(&X::g1)( pcx, 1 );
128
129 mem_fn(&X::f2)( px, 1, 2 );
130 mem_fn(&X::g2)( pcx, 1, 2 );
131
132 mem_fn(&X::f3)( px, 1, 2, 3 );
133 mem_fn(&X::g3)( pcx, 1, 2, 3 );
134
135 mem_fn(&X::f4)( px, 1, 2, 3, 4 );
136 mem_fn(&X::g4)( pcx, 1, 2, 3, 4 );
137
138 mem_fn(&X::f5)( px, 1, 2, 3, 4, 5 );
139 mem_fn(&X::g5)( pcx, 1, 2, 3, 4, 5 );
140
141 mem_fn(&X::f6)( px, 1, 2, 3, 4, 5, 6 );
142 mem_fn(&X::g6)( pcx, 1, 2, 3, 4, 5, 6 );
143
144 mem_fn(&X::f7)( px, 1, 2, 3, 4, 5, 6, 7 );
145 mem_fn(&X::g7)( pcx, 1, 2, 3, 4, 5, 6, 7 );
146
147 mem_fn(&X::f8)( px, 1, 2, 3, 4, 5, 6, 7, 8 );
148 mem_fn(&X::g8)( pcx, 1, 2, 3, 4, 5, 6, 7, 8 );
149
150 return detect_errors( hash == 2155 );
151 }
152