]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/ref.hpp" header content | |
7 | // see 'ref_test.cpp' for run-time part | |
8 | ||
9 | #include <boost/ref.hpp> | |
10 | #include <boost/core/is_same.hpp> | |
11 | #include <boost/static_assert.hpp> | |
f67539c2 | 12 | #include <boost/config/workaround.hpp> |
7c673cae FG |
13 | |
14 | namespace { | |
15 | ||
16 | template< typename T, typename U > | |
17 | void ref_test(boost::reference_wrapper<U>) | |
18 | { | |
19 | typedef typename boost::reference_wrapper<U>::type type; | |
20 | BOOST_STATIC_ASSERT((boost::core::is_same<U,type>::value)); | |
21 | BOOST_STATIC_ASSERT((boost::core::is_same<T,type>::value)); | |
22 | } | |
23 | ||
24 | template< typename T > | |
25 | void assignable_test(T x) | |
26 | { | |
27 | x = x; | |
28 | } | |
29 | ||
30 | template< bool R, typename T > | |
31 | void is_reference_wrapper_test(T) | |
32 | { | |
33 | BOOST_STATIC_ASSERT(boost::is_reference_wrapper<T>::value == R); | |
34 | } | |
35 | ||
36 | template< typename R, typename Ref > | |
37 | void cxx_reference_test(Ref) | |
38 | { | |
39 | BOOST_STATIC_ASSERT((boost::core::is_same<R,Ref>::value)); | |
40 | } | |
41 | ||
42 | template< typename R, typename Ref > | |
43 | void unwrap_reference_test(Ref) | |
44 | { | |
45 | typedef typename boost::unwrap_reference<Ref>::type type; | |
46 | BOOST_STATIC_ASSERT((boost::core::is_same<R,type>::value)); | |
47 | } | |
48 | ||
49 | } // namespace | |
50 | ||
51 | int main() | |
52 | { | |
53 | int i = 0; | |
54 | int& ri = i; | |
55 | ||
56 | int const ci = 0; | |
57 | int const& rci = ci; | |
58 | ||
59 | // 'ref/cref' functions test | |
60 | ref_test<int>(boost::ref(i)); | |
61 | ref_test<int>(boost::ref(ri)); | |
62 | ref_test<int const>(boost::ref(ci)); | |
63 | ref_test<int const>(boost::ref(rci)); | |
64 | ||
65 | ref_test<int const>(boost::cref(i)); | |
66 | ref_test<int const>(boost::cref(ri)); | |
67 | ref_test<int const>(boost::cref(ci)); | |
68 | ref_test<int const>(boost::cref(rci)); | |
69 | ||
70 | // test 'assignable' requirement | |
71 | assignable_test(boost::ref(i)); | |
72 | assignable_test(boost::ref(ri)); | |
73 | assignable_test(boost::cref(i)); | |
74 | assignable_test(boost::cref(ci)); | |
75 | assignable_test(boost::cref(rci)); | |
76 | ||
77 | // 'is_reference_wrapper' test | |
78 | is_reference_wrapper_test<true>(boost::ref(i)); | |
79 | is_reference_wrapper_test<true>(boost::ref(ri)); | |
80 | is_reference_wrapper_test<true>(boost::cref(i)); | |
81 | is_reference_wrapper_test<true>(boost::cref(ci)); | |
82 | is_reference_wrapper_test<true>(boost::cref(rci)); | |
83 | ||
84 | is_reference_wrapper_test<false>(i); | |
85 | is_reference_wrapper_test<false, int&>(ri); | |
86 | is_reference_wrapper_test<false>(ci); | |
87 | is_reference_wrapper_test<false, int const&>(rci); | |
88 | ||
89 | // ordinary references/function template arguments deduction test | |
90 | cxx_reference_test<int>(i); | |
91 | cxx_reference_test<int>(ri); | |
92 | cxx_reference_test<int>(ci); | |
93 | cxx_reference_test<int>(rci); | |
94 | ||
95 | cxx_reference_test<int&, int&>(i); | |
96 | cxx_reference_test<int&, int&>(ri); | |
97 | cxx_reference_test<int const&, int const&>(i); | |
98 | cxx_reference_test<int const&, int const&>(ri); | |
99 | cxx_reference_test<int const&, int const&>(ci); | |
100 | cxx_reference_test<int const&, int const&>(rci); | |
101 | ||
102 | // 'unwrap_reference' test | |
103 | unwrap_reference_test<int>(boost::ref(i)); | |
104 | unwrap_reference_test<int>(boost::ref(ri)); | |
105 | unwrap_reference_test<int const>(boost::cref(i)); | |
106 | unwrap_reference_test<int const>(boost::cref(ci)); | |
107 | unwrap_reference_test<int const>(boost::cref(rci)); | |
108 | ||
109 | unwrap_reference_test<int>(i); | |
110 | unwrap_reference_test<int>(ri); | |
111 | unwrap_reference_test<int>(ci); | |
112 | unwrap_reference_test<int>(rci); | |
113 | unwrap_reference_test<int&, int&>(i); | |
114 | unwrap_reference_test<int&, int&>(ri); | |
115 | unwrap_reference_test<int const&, int const&>(i); | |
116 | unwrap_reference_test<int const&, int const&>(ri); | |
117 | unwrap_reference_test<int const&, int const&>(ci); | |
118 | unwrap_reference_test<int const&, int const&>(rci); | |
119 | ||
120 | return 0; | |
121 | } |