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/core/ref.hpp>
15#include <boost/core/lightweight_test.hpp>
16
17template<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
30template<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
43int main()
44{
45 int x = 0;
46
47 test( t: x );
48 test( t: boost::ref( t&: x ) );
49 test( t: boost::cref( t: x ) );
50
51 test_nonconst( t&: x );
52
53 {
54 boost::reference_wrapper< int > r = boost::ref( t&: x );
55 test_nonconst( t&: r );
56 }
57
58 {
59 boost::reference_wrapper< int const > r = boost::cref( t: x );
60 test_nonconst( t&: r );
61 }
62
63 return boost::report_errors();
64}
65

source code of boost/libs/core/test/ref_ref_test.cpp