File: ref_ct_test.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (127 lines) | stat: -rw-r--r-- 3,787 bytes parent folder | download | duplicates (8)
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
// 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/core/ref.hpp" header content
// see 'ref_test.cpp' for run-time part

#include <boost/core/ref.hpp>
#include <boost/static_assert.hpp>
#include <boost/config/workaround.hpp>
#include <boost/type_traits/is_same.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 x1, T x2)
{
    x1 = x2;
}

template< typename T >
void assignable_test(T x)
{
    assignable_test_( 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;
}