1 //
2 // Test that ref(ref(x)) does NOT collapse to ref(x)
3 //
4 // This irregularity of std::ref is questionable and breaks
5 // existing Boost code such as proto::make_expr
6 //
7 // Copyright 2014 Peter Dimov
8 //
9 // Distributed under the Boost Software License, Version 1.0.
10 // See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt
12 //
13 
14 #include <boost/ref.hpp>
15 #include <boost/core/lightweight_test.hpp>
16 
test(T const & t)17 template<class T> void test( T const & t )
18 {
19     {
20         boost::reference_wrapper< T const > r = boost::ref( t );
21         BOOST_TEST_EQ( &r.get(), &t );
22     }
23 
24     {
25         boost::reference_wrapper< T const > r = boost::cref( t );
26         BOOST_TEST_EQ( &r.get(), &t );
27     }
28 }
29 
test_nonconst(T & t)30 template<class T> void test_nonconst( T & t )
31 {
32     {
33         boost::reference_wrapper< T > r = boost::ref( t );
34         BOOST_TEST_EQ( &r.get(), &t );
35     }
36 
37     {
38         boost::reference_wrapper< T const > r = boost::cref( t );
39         BOOST_TEST_EQ( &r.get(), &t );
40     }
41 }
42 
main()43 int main()
44 {
45     int x = 0;
46 
47     test( x );
48     test( boost::ref( x ) );
49     test( boost::cref( x ) );
50 
51     test_nonconst( x );
52 
53     {
54         boost::reference_wrapper< int > r = boost::ref( x );
55         test_nonconst( r );
56     }
57 
58     {
59         boost::reference_wrapper< int const > r = boost::cref( x );
60         test_nonconst( r );
61     }
62 
63     return boost::report_errors();
64 }
65