Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wrange-loop-construct2.C

raw
1 // PR c++/94695
2 // { dg-do compile { target c++11 } }
3 // { dg-options "-Wrange-loop-construct" }
4
5 struct Foo { };
6 struct Bar {
7 char arr[100];
8 Bar(Foo);
9 Bar(int);
10 operator int();
11 };
12
13 template<typename T>
14 struct It {
15 T operator*();
16 It operator++();
17 bool operator!=(const It);
18 };
19
20 template<typename T>
21 struct Cont {
22 using I = It<T>;
23 I begin();
24 I end();
25 };
26
27 void
28 fn1 ()
29 {
30 int arr[10];
31 Cont<int> cont_int;
32
33 for (const double &x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
34 for (const double x : arr) { (void) x; }
35 for (const int &x : arr) { (void) x; }
36 for (double &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
37 for (double x : arr) { (void) x; }
38
39 for (const int &&x : cont_int) { (void) x; }
40 for (const int &x : cont_int) { (void) x; }
41 for (const int x : cont_int) { (void) x; }
42 for (int&& x : cont_int) { (void) x; }
43 for (int x : cont_int) { (void) x; }
44
45 for (const double &&x : cont_int) { (void) x; }
46 for (const double &x : cont_int) { (void) x; }
47 for (const double x : cont_int) { (void) x; }
48
49 for (double &&x : cont_int) { (void) x; }
50 for (double x : cont_int) { (void) x; }
51
52 for (const Bar &&x : cont_int) { (void) x; }
53 for (const Bar x : cont_int) { (void) x; }
54 }
55
56 void
57 fn2 ()
58 {
59 Cont<int &> cont_int_ref;
60
61 for (const int &x : cont_int_ref) { (void) x; }
62 for (const int x : cont_int_ref) { (void) x; }
63 for (int &x : cont_int_ref) { (void) x; }
64 for (int x : cont_int_ref) { (void) x; }
65
66 for (const double &&x : cont_int_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
67 for (const double &x : cont_int_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
68 for (const double x : cont_int_ref) { (void) x; }
69 for (double &&x : cont_int_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
70 for (double x : cont_int_ref) { (void) x; }
71
72 for (const Bar &&x : cont_int_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
73 for (const Bar &x : cont_int_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
74 for (const Bar x : cont_int_ref) { (void) x; }
75 for (Bar &&x : cont_int_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
76 for (Bar x : cont_int_ref) { (void) x; }
77 }
78
79 void
80 fn3 ()
81 {
82 Cont<Bar> cont_bar;
83
84 for (const Bar &&x : cont_bar) { (void) x; }
85 for (const Bar &x : cont_bar) { (void) x; }
86 for (const Bar x : cont_bar) { (void) x; }
87 for (Bar &&x : cont_bar) { (void) x; }
88 for (Bar x : cont_bar) { (void) x; }
89
90 for (const int &&x : cont_bar) { (void) x; }
91 for (const int &x : cont_bar) { (void) x; }
92 for (const int x : cont_bar) { (void) x; }
93 for (int &&x : cont_bar) { (void) x; }
94 for (int x : cont_bar) { (void) x; }
95 }
96
97 void
98 fn4 ()
99 {
100 Cont<Bar &> cont_bar_ref;
101
102 for (const Bar &x : cont_bar_ref) { (void) x; }
103 for (Bar &x : cont_bar_ref) { (void) x; }
104 for (Bar x : cont_bar_ref) { (void) x; }
105
106 for (const int &&x : cont_bar_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
107 for (const int &x : cont_bar_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
108 for (const int x : cont_bar_ref) { (void) x; }
109 for (int &&x : cont_bar_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
110 for (int x : cont_bar_ref) { (void) x; }
111 }
112
113 void
114 fn5 ()
115 {
116 Cont<Foo> cont_foo;
117
118 for (const Bar &&x : cont_foo) { (void) x; }
119 for (const Bar &x : cont_foo) { (void) x; }
120 for (const Bar x : cont_foo) { (void) x; }
121 for (Bar &&x : cont_foo) { (void) x; }
122 for (Bar x : cont_foo) { (void) x; }
123 }
124
125 void
126 fn6 ()
127 {
128 Cont<Foo &> cont_foo_ref;
129
130 for (const Bar &&x : cont_foo_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
131 for (const Bar &x : cont_foo_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
132 for (const Bar x : cont_foo_ref) { (void) x; }
133 for (Bar &&x : cont_foo_ref) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
134 for (Bar x : cont_foo_ref) { (void) x; }
135 }
136
137 void
138 fn7 ()
139 {
140 double arr[2];
141
142 for (const double &x : arr) { (void) x; }
143 for (const double x : arr) { (void) x; }
144 for (double &x : arr) { (void) x; }
145 for (double x : arr) { (void) x; }
146
147 for (const int &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
148 for (const int &x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
149 for (const int x : arr) { (void) x; }
150 for (int &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
151 for (int x : arr) { (void) x; }
152
153 for (const Bar &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
154 for (const Bar &x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
155 for (const Bar x : arr) { (void) x; }
156 for (Bar &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
157 for (Bar x : arr) { (void) x; }
158 }
159
160 void
161 fn8 ()
162 {
163 Foo arr[2];
164
165 for (const Foo &x : arr) { (void) x; }
166 for (const Foo x : arr) { (void) x; }
167 for (Foo &x : arr) { (void) x; }
168 for (Foo x : arr) { (void) x; }
169
170 for (const Bar &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
171 for (const Bar &x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
172 for (const Bar x : arr) { (void) x; }
173 for (Bar &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
174 for (Bar x : arr) { (void) x; }
175 }
176
177 void
178 fn9 ()
179 {
180 Bar arr[2] = { 1, 2 };
181
182 for (const Bar &x : arr) { (void) x; }
183 for (Bar &x : arr) { (void) x; }
184 for (Bar x : arr) { (void) x; }
185
186 for (const int &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
187 for (const int &x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
188 for (const int x : arr) { (void) x; }
189 for (int &&x : arr) { (void) x; } // { dg-warning "binds to a temporary constructed from type" }
190 for (int x : arr) { (void) x; }
191 }
192
193 template<typename T>
194 void
195 fn10 ()
196 {
197 Cont<Bar> cont_bar;
198
199 for (const Bar &x : cont_bar) { (void) x; }
200
201 Cont<T> cont_dep;
202 for (const T &x : cont_dep) { (void) x; }
203 }
204 template void fn10<Bar>();
205
206 struct S {
207 void fn()
208 {
209 Cont<Bar> cont_bar;
210 for (const Bar &x : cont_bar) { (void) x; }
211 }
212 };