1 | // |
2 | // mem_fn_ref_test.cpp - reference_wrapper |
3 | // |
4 | // Copyright 2009, 2024 Peter Dimov |
5 | // |
6 | // Distributed under the Boost Software License, Version 1.0. |
7 | // See accompanying file LICENSE_1_0.txt or copy at |
8 | // http://www.boost.org/LICENSE_1_0.txt |
9 | // |
10 | |
11 | #if defined(__GNUC__) && defined(__SANITIZE_ADDRESS__) |
12 | # pragma GCC diagnostic ignored "-Warray-bounds" |
13 | #endif |
14 | |
15 | #include <boost/mem_fn.hpp> |
16 | #include <boost/ref.hpp> |
17 | #include <boost/core/lightweight_test.hpp> |
18 | |
19 | struct X |
20 | { |
21 | int f0() { return 1; } |
22 | int g0() const { return -1; } |
23 | |
24 | int f1( int x1 ) { return x1; } |
25 | int g1( int x1 ) const { return -x1; } |
26 | |
27 | int f2( int x1, int x2 ) { return x1+x2; } |
28 | int g2( int x1, int x2 ) const { return -x1-x2; } |
29 | |
30 | int f3( int x1, int x2, int x3 ) { return x1+x2+x3; } |
31 | int g3( int x1, int x2, int x3 ) const { return -x1-x2-x3; } |
32 | }; |
33 | |
34 | int main() |
35 | { |
36 | X x; |
37 | |
38 | BOOST_TEST_EQ( boost::mem_fn( &X::f0 )( boost::ref( x ) ), 1 ); |
39 | BOOST_TEST_EQ( boost::mem_fn( &X::g0 )( boost::cref( x ) ), -1 ); |
40 | |
41 | BOOST_TEST_EQ( boost::mem_fn( &X::f1 )( boost::ref( x ), 1 ), 1 ); |
42 | BOOST_TEST_EQ( boost::mem_fn( &X::g1 )( boost::cref( x ), 1 ), -1 ); |
43 | |
44 | BOOST_TEST_EQ( boost::mem_fn( &X::f2 )( boost::ref( x ), 1, 2 ), 1+2 ); |
45 | BOOST_TEST_EQ( boost::mem_fn( &X::g2 )( boost::cref( x ), 1, 2 ), -1-2 ); |
46 | |
47 | BOOST_TEST_EQ( boost::mem_fn( &X::f3 )( boost::ref( x ), 1, 2, 3 ), 1+2+3 ); |
48 | BOOST_TEST_EQ( boost::mem_fn( &X::g3 )( boost::cref( x ), 1, 2, 3 ), -1-2-3 ); |
49 | |
50 | return boost::report_errors(); |
51 | } |
52 | |