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