File: ref_ct_test.cpp

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (115 lines) | stat: -rw-r--r-- 3,431 bytes parent folder | download
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
// 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/same_traits.hpp>
#include <boost/static_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)
{
    BOOST_STATIC_ASSERT((boost::is_same<R,Ref>::value));
}

template< typename R, typename Ref >
void unwrap_reference_test(Ref)
{
    typedef typename boost::unwrap_reference<Ref>::type type;
    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;
}