summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp0x/reference_collapsing.C
blob: 5b682e304598b586a6e498c01521cb85ddc937eb (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// I, Howard Hinnant, hereby place this code in the public domain.

// Test the reference collapsing rules.  Note that there are recent differences
//    for how cv-qualifications are applied to reference types. 7.1.3, 14.3.1

// { dg-do compile }
// { dg-options "-std=c++0x" }

template <bool> struct sa;
template <> struct sa<true> {};

template <class T, T v>
struct integral_constant
{
	static const T                  value = v;
	typedef T                       value_type;
	typedef integral_constant<T, v> type;
};

typedef integral_constant<bool, true>  true_type;
typedef integral_constant<bool, false> false_type;

template <class T> struct is_lvalue_reference     : public integral_constant<bool, false> {};
template <class T> struct is_lvalue_reference<T&> : public integral_constant<bool, true> {};

template <class T> struct is_rvalue_reference      : public integral_constant<bool, false> {};
template <class T> struct is_rvalue_reference<T&&> : public integral_constant<bool, true> {};

template <class T> struct remove_reference      {typedef T type;};
template <class T> struct remove_reference<T&>  {typedef T type;};
template <class T> struct remove_reference<T&&> {typedef T type;};

template <class T> struct is_const          : public integral_constant<bool, false> {};
template <class T> struct is_const<T const> : public integral_constant<bool, true> {};

template <class T> struct is_volatile             : public integral_constant<bool, false> {};
template <class T> struct is_volatile<T volatile> : public integral_constant<bool, true> {};

struct A {};

typedef A& Alref;
typedef const A& cAlref;
typedef volatile A& vAlref;
typedef const volatile A& cvAlref;

typedef A&& Arref;
typedef const A&& cArref;
typedef volatile A&& vArref;
typedef const volatile A&& cvArref;

template <class T, bool is_lvalue_ref, bool is_rvalue_ref, bool s_const, bool s_volatile>
void test()
{
    sa<is_lvalue_reference<T>::value == is_lvalue_ref> t1;
    sa<is_rvalue_reference<T>::value == is_rvalue_ref> t2;
    sa<is_const   <typename remove_reference<T>::type>::value == s_const>    t3;
    sa<is_volatile<typename remove_reference<T>::type>::value == s_volatile> t4;
    sa<is_const   <typename remove_reference<const          T>::type>::value == s_const   > t5;
    sa<is_volatile<typename remove_reference<      volatile T>::type>::value == s_volatile> t6;
}

int main()
{
    // sanity check
    test<               A&,   true, false, false, false>();
    test<const          A&,   true, false,  true, false>();
    test<      volatile A&,   true, false, false,  true>();
    test<const volatile A&,   true, false,  true,  true>();
    test<               A&&, false,  true, false, false>();
    test<const          A&&, false,  true,  true, false>();
    test<      volatile A&&, false,  true, false,  true>();
    test<const volatile A&&, false,  true,  true,  true>();

// lvalue reference test

    // Alref
    test<               Alref&,  true, false, false, false>();
    test<const          Alref&,  true, false, false, false>();
    test<      volatile Alref&,  true, false, false, false>();
    test<const volatile Alref&,  true, false, false, false>();

    // cAlref
    test<               cAlref&,  true, false,  true, false>();
    test<const          cAlref&,  true, false,  true, false>();
    test<      volatile cAlref&,  true, false,  true, false>();
    test<const volatile cAlref&,  true, false,  true, false>();

    // vAlref
    test<               vAlref&,  true, false, false,  true>();
    test<const          vAlref&,  true, false, false,  true>();
    test<      volatile vAlref&,  true, false, false,  true>();
    test<const volatile vAlref&,  true, false, false,  true>();

    // cvAlref
    test<               cvAlref&,  true, false,  true,  true>();
    test<const          cvAlref&,  true, false,  true,  true>();
    test<      volatile cvAlref&,  true, false,  true,  true>();
    test<const volatile cvAlref&,  true, false,  true,  true>();

    // Arref
    test<               Arref&,  true, false, false, false>();
    test<const          Arref&,  true, false, false, false>();
    test<      volatile Arref&,  true, false, false, false>();
    test<const volatile Arref&,  true, false, false, false>();

    // cArref
    test<               cArref&,  true, false,  true, false>();
    test<const          cArref&,  true, false,  true, false>();
    test<      volatile cArref&,  true, false,  true, false>();
    test<const volatile cArref&,  true, false,  true, false>();

    // vArref
    test<               vArref&,  true, false, false,  true>();
    test<const          vArref&,  true, false, false,  true>();
    test<      volatile vArref&,  true, false, false,  true>();
    test<const volatile vArref&,  true, false, false,  true>();

    // vArref
    test<               cvArref&,  true, false,  true,  true>();
    test<const          cvArref&,  true, false,  true,  true>();
    test<      volatile cvArref&,  true, false,  true,  true>();
    test<const volatile cvArref&,  true, false,  true,  true>();

// rvalue reference test

    // Alref
    test<               Alref&&,  true, false, false, false>();
    test<const          Alref&&,  true, false, false, false>();
    test<      volatile Alref&&,  true, false, false, false>();
    test<const volatile Alref&&,  true, false, false, false>();

    // cAlref
    test<               cAlref&&,  true, false,  true, false>();
    test<const          cAlref&&,  true, false,  true, false>();
    test<      volatile cAlref&&,  true, false,  true, false>();
    test<const volatile cAlref&&,  true, false,  true, false>();

    // vAlref
    test<               vAlref&&,  true, false, false,  true>();
    test<const          vAlref&&,  true, false, false,  true>();
    test<      volatile vAlref&&,  true, false, false,  true>();
    test<const volatile vAlref&&,  true, false, false,  true>();

    // cvAlref
    test<               cvAlref&&,  true, false,  true,  true>();
    test<const          cvAlref&&,  true, false,  true,  true>();
    test<      volatile cvAlref&&,  true, false,  true,  true>();
    test<const volatile cvAlref&&,  true, false,  true,  true>();

    // Arref
    test<               Arref&&, false,  true, false, false>();
    test<const          Arref&&, false,  true, false, false>();
    test<      volatile Arref&&, false,  true, false, false>();
    test<const volatile Arref&&, false,  true, false, false>();

    // cArref
    test<               cArref&&, false,  true,  true, false>();
    test<const          cArref&&, false,  true,  true, false>();
    test<      volatile cArref&&, false,  true,  true, false>();
    test<const volatile cArref&&, false,  true,  true, false>();

    // vArref
    test<               vArref&&, false,  true, false,  true>();
    test<const          vArref&&, false,  true, false,  true>();
    test<      volatile vArref&&, false,  true, false,  true>();
    test<const volatile vArref&&, false,  true, false,  true>();

    // cvArref
    test<               cvArref&&, false,  true,  true,  true>();
    test<const          cvArref&&, false,  true,  true,  true>();
    test<      volatile cvArref&&, false,  true,  true,  true>();
    test<const volatile cvArref&&, false,  true,  true,  true>();

    return 0;
}