File: ref_ct_test.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (134 lines) | stat: -rw-r--r-- 4,109 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright David Abrahams and Aleksey Gurtovoy
// 2002-2004. Distributed under the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// compile-time test for "boost/ref.hpp" header content
// see 'ref_test.cpp' for run-time part

#include <boost/ref.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/static_assert.hpp>
#include <boost/detail/workaround.hpp>

#include <boost/mpl/assert.hpp>

namespace {

template< typename T, typename U >
void ref_test(boost::reference_wrapper<U>)
{
    typedef typename boost::reference_wrapper<U>::type type;
    BOOST_STATIC_ASSERT((boost::is_same<U,type>::value));
    BOOST_STATIC_ASSERT((boost::is_same<T,type>::value));
}

template< typename T >
void assignable_test(T x)
{
    x = x;
}

template< bool R, typename T >
void is_reference_wrapper_test(T)
{
    BOOST_STATIC_ASSERT(boost::is_reference_wrapper<T>::value == R);
}

template< typename R, typename Ref >
void cxx_reference_test(Ref)
{
#if BOOST_WORKAROUND(__BORLANDC__, < 0x600)
    typedef typename boost::remove_const<Ref>::type ref;
    BOOST_STATIC_ASSERT((boost::is_same<R,ref>::value));
#else
    BOOST_STATIC_ASSERT((boost::is_same<R,Ref>::value));
#endif
}

template< typename R, typename Ref >
void unwrap_reference_test(Ref)
{
#if BOOST_WORKAROUND(__BORLANDC__, < 0x600)
    typedef typename boost::remove_const<Ref>::type ref;
    typedef typename boost::unwrap_reference<ref>::type type;
#else
    typedef typename boost::unwrap_reference<Ref>::type type;
#endif
    BOOST_STATIC_ASSERT((boost::is_same<R,type>::value));
}

} // namespace

int main()
{
    int i = 0;
    int& ri = i;

    int const ci = 0;
    int const& rci = ci;

    // 'ref/cref' functions test
    ref_test<int>(boost::ref(i));
    ref_test<int>(boost::ref(ri));
    ref_test<int const>(boost::ref(ci));
    ref_test<int const>(boost::ref(rci));

    ref_test<int const>(boost::cref(i));
    ref_test<int const>(boost::cref(ri));
    ref_test<int const>(boost::cref(ci));
    ref_test<int const>(boost::cref(rci));

    // test 'assignable' requirement
    assignable_test(boost::ref(i));
    assignable_test(boost::ref(ri));
    assignable_test(boost::cref(i));
    assignable_test(boost::cref(ci));
    assignable_test(boost::cref(rci));

    // 'is_reference_wrapper' test
    is_reference_wrapper_test<true>(boost::ref(i));
    is_reference_wrapper_test<true>(boost::ref(ri));
    is_reference_wrapper_test<true>(boost::cref(i));
    is_reference_wrapper_test<true>(boost::cref(ci));
    is_reference_wrapper_test<true>(boost::cref(rci));

    is_reference_wrapper_test<false>(i);
    is_reference_wrapper_test<false, int&>(ri);
    is_reference_wrapper_test<false>(ci);
    is_reference_wrapper_test<false, int const&>(rci);

    // ordinary references/function template arguments deduction test
    cxx_reference_test<int>(i);
    cxx_reference_test<int>(ri);
    cxx_reference_test<int>(ci);
    cxx_reference_test<int>(rci);

    cxx_reference_test<int&, int&>(i);
    cxx_reference_test<int&, int&>(ri);
    cxx_reference_test<int const&, int const&>(i);
    cxx_reference_test<int const&, int const&>(ri);
    cxx_reference_test<int const&, int const&>(ci);
    cxx_reference_test<int const&, int const&>(rci);

    // 'unwrap_reference' test
    unwrap_reference_test<int>(boost::ref(i));
    unwrap_reference_test<int>(boost::ref(ri));
    unwrap_reference_test<int const>(boost::cref(i));
    unwrap_reference_test<int const>(boost::cref(ci));
    unwrap_reference_test<int const>(boost::cref(rci));

    unwrap_reference_test<int>(i);
    unwrap_reference_test<int>(ri);
    unwrap_reference_test<int>(ci);
    unwrap_reference_test<int>(rci);
    unwrap_reference_test<int&, int&>(i);
    unwrap_reference_test<int&, int&>(ri);
    unwrap_reference_test<int const&, int const&>(i);
    unwrap_reference_test<int const&, int const&>(ri);
    unwrap_reference_test<int const&, int const&>(ci);
    unwrap_reference_test<int const&, int const&>(rci);

    return 0;
}