1// Copyright David Abrahams and Aleksey Gurtovoy
2// 2002-2004. Distributed under the Boost Software License, Version
3// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6// compile-time test for "boost/core/ref.hpp" header content
7// see 'ref_test.cpp' for run-time part
8
9#include <boost/core/ref.hpp>
10#include <boost/static_assert.hpp>
11#include <boost/config/workaround.hpp>
12#include <boost/type_traits/is_same.hpp>
13
14namespace {
15
16template< typename T, typename U >
17void ref_test(boost::reference_wrapper<U>)
18{
19 typedef typename boost::reference_wrapper<U>::type type;
20 BOOST_STATIC_ASSERT((boost::is_same<U,type>::value));
21 BOOST_STATIC_ASSERT((boost::is_same<T,type>::value));
22}
23
24template< typename T >
25void assignable_test_(T x1, T x2)
26{
27 x1 = x2;
28}
29
30template< typename T >
31void assignable_test(T x)
32{
33 assignable_test_( x, x );
34}
35
36template< bool R, typename T >
37void is_reference_wrapper_test(T)
38{
39 BOOST_STATIC_ASSERT(boost::is_reference_wrapper<T>::value == R);
40}
41
42template< typename R, typename Ref >
43void cxx_reference_test(Ref)
44{
45 BOOST_STATIC_ASSERT((boost::is_same<R,Ref>::value));
46}
47
48template< typename R, typename Ref >
49void unwrap_reference_test(Ref)
50{
51 typedef typename boost::unwrap_reference<Ref>::type type;
52 BOOST_STATIC_ASSERT((boost::is_same<R,type>::value));
53}
54
55} // namespace
56
57int main()
58{
59 int i = 0;
60 int& ri = i;
61
62 int const ci = 0;
63 int const& rci = ci;
64
65 // 'ref/cref' functions test
66 ref_test<int>(boost::ref(t&: i));
67 ref_test<int>(boost::ref(t&: ri));
68 ref_test<int const>(boost::ref(t: ci));
69 ref_test<int const>(boost::ref(t: rci));
70
71 ref_test<int const>(boost::cref(t: i));
72 ref_test<int const>(boost::cref(t: ri));
73 ref_test<int const>(boost::cref(t: ci));
74 ref_test<int const>(boost::cref(t: rci));
75
76 // test 'assignable' requirement
77 assignable_test(x: boost::ref(t&: i));
78 assignable_test(x: boost::ref(t&: ri));
79 assignable_test(x: boost::cref(t: i));
80 assignable_test(x: boost::cref(t: ci));
81 assignable_test(x: boost::cref(t: rci));
82
83 // 'is_reference_wrapper' test
84 is_reference_wrapper_test<true>(boost::ref(t&: i));
85 is_reference_wrapper_test<true>(boost::ref(t&: ri));
86 is_reference_wrapper_test<true>(boost::cref(t: i));
87 is_reference_wrapper_test<true>(boost::cref(t: ci));
88 is_reference_wrapper_test<true>(boost::cref(t: rci));
89
90 is_reference_wrapper_test<false>(i);
91 is_reference_wrapper_test<false, int&>(ri);
92 is_reference_wrapper_test<false>(ci);
93 is_reference_wrapper_test<false, int const&>(rci);
94
95 // ordinary references/function template arguments deduction test
96 cxx_reference_test<int>(i);
97 cxx_reference_test<int>(ri);
98 cxx_reference_test<int>(ci);
99 cxx_reference_test<int>(rci);
100
101 cxx_reference_test<int&, int&>(i);
102 cxx_reference_test<int&, int&>(ri);
103 cxx_reference_test<int const&, int const&>(i);
104 cxx_reference_test<int const&, int const&>(ri);
105 cxx_reference_test<int const&, int const&>(ci);
106 cxx_reference_test<int const&, int const&>(rci);
107
108 // 'unwrap_reference' test
109 unwrap_reference_test<int>(boost::ref(t&: i));
110 unwrap_reference_test<int>(boost::ref(t&: ri));
111 unwrap_reference_test<int const>(boost::cref(t: i));
112 unwrap_reference_test<int const>(boost::cref(t: ci));
113 unwrap_reference_test<int const>(boost::cref(t: rci));
114
115 unwrap_reference_test<int>(i);
116 unwrap_reference_test<int>(ri);
117 unwrap_reference_test<int>(ci);
118 unwrap_reference_test<int>(rci);
119 unwrap_reference_test<int&, int&>(i);
120 unwrap_reference_test<int&, int&>(ri);
121 unwrap_reference_test<int const&, int const&>(i);
122 unwrap_reference_test<int const&, int const&>(ri);
123 unwrap_reference_test<int const&, int const&>(ci);
124 unwrap_reference_test<int const&, int const&>(rci);
125
126 return 0;
127}
128

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