Debian Codegraph @

gcc/testsuite/g++.dg/cpp23/explicit-obj-constraints2.C

raw
1 // P0847R7
2 // { dg-do run { target c++23 } }
3
4 // overload resolution of static/xobj and iobj/xobj non-template member functions
5 // with constraints in a class template
6
7 template<typename T>
8 concept Constrain = true;
9
10 inline constexpr int iobj_fn = 5;
11 inline constexpr int xobj_fn = 10;
12 inline constexpr int static_fn = 20;
13
14 // first 2 letters are the order of the definitions
15 // the constraint applies to the first definition,
16 // for *_r cases the constraint applies to the second
17
18 template<typename T>
19 struct S {
20 // xobj/static
21 int f_xs_v(this S) requires Constrain<T> { return xobj_fn; };
22 static int f_xs_v() { return static_fn; };
23
24 int f_xs_ref(this S&) requires Constrain<T> { return xobj_fn; };
25 static int f_xs_ref() { return static_fn; };
26
27 int f_xs_cref(this S const&) requires Constrain<T> { return xobj_fn; };
28 static int f_xs_cref() { return static_fn; };
29
30 int f_xs_rref(this S&&) requires Constrain<T> { return xobj_fn; };
31 static int f_xs_rref() { return static_fn; };
32
33 int f_xs_crref(this S const&&) requires Constrain<T> { return xobj_fn; };
34 static int f_xs_crref() { return static_fn; };
35
36 int f_xs_dv(this auto) requires Constrain<T> { return xobj_fn; };
37 static int f_xs_dv() { return static_fn; };
38
39 int f_xs_dcref(this auto const&) requires Constrain<T> { return xobj_fn; };
40 static int f_xs_dcref() { return static_fn; };
41
42 int f_xs_dfwdref(this auto&&) requires Constrain<T> { return xobj_fn; };
43 static int f_xs_dfwdref() { return static_fn; };
44
45 // _r
46 int f_xs_v_r(this S) { return xobj_fn; };
47 static int f_xs_v_r() requires Constrain<T> { return static_fn; };
48
49 int f_xs_ref_r(this S&) { return xobj_fn; };
50 static int f_xs_ref_r() requires Constrain<T> { return static_fn; };
51
52 int f_xs_cref_r(this S const&) { return xobj_fn; };
53 static int f_xs_cref_r() requires Constrain<T> { return static_fn; };
54
55 int f_xs_rref_r(this S&&) { return xobj_fn; };
56 static int f_xs_rref_r() requires Constrain<T> { return static_fn; };
57
58 int f_xs_crref_r(this S const&&) { return xobj_fn; };
59 static int f_xs_crref_r() requires Constrain<T> { return static_fn; };
60
61 int f_xs_dv_r(this auto) { return xobj_fn; };
62 static int f_xs_dv_r() requires Constrain<T> { return static_fn; };
63
64 int f_xs_dcref_r(this auto const&) { return xobj_fn; };
65 static int f_xs_dcref_r() requires Constrain<T> { return static_fn; };
66
67 int f_xs_dfwdref_r(this auto&&) { return xobj_fn; };
68 static int f_xs_dfwdref_r() requires Constrain<T> { return static_fn; };
69
70 // static/xobj
71 static int f_sx_v() requires Constrain<T> { return static_fn; };
72 int f_sx_v(this S) { return xobj_fn; };
73
74 static int f_sx_ref() requires Constrain<T> { return static_fn; };
75 int f_sx_ref(this S&) { return xobj_fn; };
76
77 static int f_sx_cref() requires Constrain<T> { return static_fn; };
78 int f_sx_cref(this S const&) { return xobj_fn; };
79
80 static int f_sx_rref() requires Constrain<T> { return static_fn; };
81 int f_sx_rref(this S&&) { return xobj_fn; };
82
83 static int f_sx_crref() requires Constrain<T> { return static_fn; };
84 int f_sx_crref(this S const&&) { return xobj_fn; };
85
86 static int f_sx_dv() requires Constrain<T> { return static_fn; };
87 int f_sx_dv(this auto) { return xobj_fn; };
88
89 static int f_sx_dcref() requires Constrain<T> { return static_fn; };
90 int f_sx_dcref(this auto const&) { return xobj_fn; };
91
92 static int f_sx_dfwdref() requires Constrain<T> { return static_fn; };
93 int f_sx_dfwdref(this auto&&) { return xobj_fn; };
94
95 // _r
96 static int f_sx_v_r() { return static_fn; };
97 int f_sx_v_r(this S) requires Constrain<T> { return xobj_fn; };
98
99 static int f_sx_ref_r() { return static_fn; };
100 int f_sx_ref_r(this S&) requires Constrain<T> { return xobj_fn; };
101
102 static int f_sx_cref_r() { return static_fn; };
103 int f_sx_cref_r(this S const&) requires Constrain<T> { return xobj_fn; };
104
105 static int f_sx_rref_r() { return static_fn; };
106 int f_sx_rref_r(this S&&) requires Constrain<T> { return xobj_fn; };
107
108 static int f_sx_crref_r() { return static_fn; };
109 int f_sx_crref_r(this S const&&) requires Constrain<T> { return xobj_fn; };
110
111 static int f_sx_dv_r() { return static_fn; };
112 int f_sx_dv_r(this auto) requires Constrain<T> { return xobj_fn; };
113
114 static int f_sx_dcref_r() { return static_fn; };
115 int f_sx_dcref_r(this auto const&) requires Constrain<T> { return xobj_fn; };
116
117 static int f_sx_dfwdref_r() { return static_fn; };
118 int f_sx_dfwdref_r(this auto&&) requires Constrain<T> { return xobj_fn; };
119
120 // xobj/iobj with matching object parameters
121
122 // We are only testing constraints here, so we need parameter lists
123 // to match, which means we need corresponding object parameters.
124 // Remember, the rules for object parameter correspondence are weird.
125 // ([basic.scope.scope-3.1])
126 //
127 // NOTE: CWG2789 does not specify this properly, I am implementing it
128 // assuming the above correspondence rules
129
130 // *_refqual the iobj member function has a reference qualifier
131 // *_r the constraint applies to the second definition
132
133 // ix
134 int f_ix_m0() requires Constrain<T> { return iobj_fn; };
135 int f_ix_m0(this S&) { return xobj_fn; };
136 // See note
137 int f_ix_m1() requires Constrain<T> { return iobj_fn; };
138 int f_ix_m1(this S&&) { return xobj_fn; };
139
140 int f_ix_c0() const requires Constrain<T> { return iobj_fn; };
141 int f_ix_c0(this S const&) { return xobj_fn; };
142 // See note
143 int f_ix_c1() const requires Constrain<T> { return iobj_fn; };
144 int f_ix_c1(this S const&&) { return xobj_fn; };
145
146 // xi
147 int f_xi_m0(this S&) requires Constrain<T> { return xobj_fn; };
148 int f_xi_m0() { return iobj_fn; };
149 // See note
150 int f_xi_m1(this S&&) requires Constrain<T> { return xobj_fn; };
151 int f_xi_m1() { return iobj_fn; };
152
153 int f_xi_c0(this S const&) requires Constrain<T> { return xobj_fn; };
154 int f_xi_c0() const { return iobj_fn; };
155 // See note
156 int f_xi_c1(this S const&&) requires Constrain<T> { return xobj_fn; };
157 int f_xi_c1() const { return iobj_fn; };
158
159 // with ref qualifier
160
161 // ix
162 int f_ix_m0_refqual() & requires Constrain<T> { return iobj_fn; };
163 int f_ix_m0_refqual(this S&) { return xobj_fn; };
164
165 int f_ix_m1_refqual() && requires Constrain<T> { return iobj_fn; };
166 int f_ix_m1_refqual(this S&&) { return xobj_fn; };
167
168 int f_ix_c0_refqual() const& requires Constrain<T> { return iobj_fn; };
169 int f_ix_c0_refqual(this S const&) { return xobj_fn; };
170
171 int f_ix_c1_refqual() const&& requires Constrain<T> { return iobj_fn; };
172 int f_ix_c1_refqual(this S const&&) { return xobj_fn; };
173
174 // xi
175 int f_xi_m0_refqual(this S&) requires Constrain<T> { return xobj_fn; };
176 int f_xi_m0_refqual() & { return iobj_fn; };
177
178 int f_xi_m1_refqual(this S&&) requires Constrain<T> { return xobj_fn; };
179 int f_xi_m1_refqual() && { return iobj_fn; };
180
181 int f_xi_c0_refqual(this S const&) requires Constrain<T> { return xobj_fn; };
182 int f_xi_c0_refqual() const& { return iobj_fn; };
183
184 int f_xi_c1_refqual(this S const&&) requires Constrain<T> { return xobj_fn; };
185 int f_xi_c1_refqual() const&& { return iobj_fn; };
186
187 // _r without ref qualifier
188
189 // ix
190 int f_ix_m0_r() { return iobj_fn; };
191 int f_ix_m0_r(this S&) requires Constrain<T> { return xobj_fn; };
192 // See note
193 int f_ix_m1_r() { return iobj_fn; };
194 int f_ix_m1_r(this S&&) requires Constrain<T> { return xobj_fn; };
195
196 int f_ix_c0_r() const { return iobj_fn; };
197 int f_ix_c0_r(this S const&) requires Constrain<T> { return xobj_fn; };
198 // See note
199 int f_ix_c1_r() const { return iobj_fn; };
200 int f_ix_c1_r(this S const&&) requires Constrain<T> { return xobj_fn; };
201
202 // xi
203 int f_xi_m0_r(this S&) { return xobj_fn; };
204 int f_xi_m0_r() requires Constrain<T> { return iobj_fn; };
205 // See note
206 int f_xi_m1_r(this S&&) { return xobj_fn; };
207 int f_xi_m1_r() requires Constrain<T> { return iobj_fn; };
208
209 int f_xi_c0_r(this S const&) { return xobj_fn; };
210 int f_xi_c0_r() const requires Constrain<T> { return iobj_fn; };
211 // See note
212 int f_xi_c1_r(this S const&&) { return xobj_fn; };
213 int f_xi_c1_r() const requires Constrain<T> { return iobj_fn; };
214
215 // _r with ref qualifier
216 // ix
217 int f_ix_m0_refqual_r() & { return iobj_fn; };
218 int f_ix_m0_refqual_r(this S&) requires Constrain<T> { return xobj_fn; };
219
220 int f_ix_m1_refqual_r() && { return iobj_fn; };
221 int f_ix_m1_refqual_r(this S&&) requires Constrain<T> { return xobj_fn; };
222
223 int f_ix_c0_refqual_r() const& { return iobj_fn; };
224 int f_ix_c0_refqual_r(this S const&) requires Constrain<T> { return xobj_fn; };
225
226 int f_ix_c1_refqual_r() const&& { return iobj_fn; };
227 int f_ix_c1_refqual_r(this S const&&) requires Constrain<T> { return xobj_fn; };
228
229 // xi
230 int f_xi_m0_refqual_r(this S&) { return xobj_fn; };
231 int f_xi_m0_refqual_r() & requires Constrain<T> { return iobj_fn; };
232
233 int f_xi_m1_refqual_r(this S&&) { return xobj_fn; };
234 int f_xi_m1_refqual_r() && requires Constrain<T> { return iobj_fn; };
235
236 int f_xi_c0_refqual_r(this S const&) { return xobj_fn; };
237 int f_xi_c0_refqual_r() const& requires Constrain<T> { return iobj_fn; };
238
239 int f_xi_c1_refqual_r(this S const&&) { return xobj_fn; };
240 int f_xi_c1_refqual_r() const&& requires Constrain<T> { return iobj_fn; };
241 };
242
243 int main()
244 {
245 // The commented out cases are ambiguous, which is most likely the correct
246 // behavior. It is something that I want to propose to change, and I want
247 // to leave them in as they are a little weird.
248 //
249 // Furthermore, as the comment at the top of this file indicates, I am not
250 // clear on the correct behavior of the static/xobj cases in general.
251 using S = S<int>;
252 S s{};
253 if (s.f_xs_v () != xobj_fn)
254 __builtin_abort ();
255 if (s.f_xs_ref () != xobj_fn)
256 __builtin_abort ();
257 if (s.f_xs_cref () != xobj_fn)
258 __builtin_abort ();
259 if (static_cast<S&&>(s).f_xs_rref () != xobj_fn)
260 __builtin_abort ();
261 if (static_cast<S&&>(s).f_xs_crref () != xobj_fn)
262 __builtin_abort ();
263 // if (s.f_xs_dv () != xobj_fn)
264 // __builtin_abort ();
265 // if (s.f_xs_dcref () != xobj_fn)
266 // __builtin_abort ();
267 // if (s.f_xs_dfwdref () != xobj_fn)
268 // __builtin_abort ();
269
270 if (s.f_xs_v_r () != static_fn)
271 __builtin_abort ();
272 if (s.f_xs_ref_r () != static_fn)
273 __builtin_abort ();
274 if (s.f_xs_cref_r () != static_fn)
275 __builtin_abort ();
276 if (static_cast<S&&>(s).f_xs_rref_r () != static_fn)
277 __builtin_abort ();
278 if (static_cast<S&&>(s).f_xs_crref_r () != static_fn)
279 __builtin_abort ();
280 // if (s.f_xs_dv_r () != static_fn)
281 // __builtin_abort ();
282 // if (s.f_xs_dcref_r () != static_fn)
283 // __builtin_abort ();
284 // if (s.f_xs_dfwdref_r () != static_fn)
285 // __builtin_abort ();
286
287 if (s.f_sx_v () != static_fn)
288 __builtin_abort ();
289 if (s.f_sx_ref () != static_fn)
290 __builtin_abort ();
291 if (s.f_sx_cref () != static_fn)
292 __builtin_abort ();
293 if (static_cast<S&&>(s).f_sx_rref () != static_fn)
294 __builtin_abort ();
295 if (static_cast<S&&>(s).f_sx_crref () != static_fn)
296 __builtin_abort ();
297 // if (s.f_sx_dv () != static_fn)
298 // __builtin_abort ();
299 // if (s.f_sx_dcref () != static_fn)
300 // __builtin_abort ();
301 // if (s.f_sx_dfwdref () != static_fn)
302 // __builtin_abort ();
303
304 if (s.f_sx_v_r () != xobj_fn)
305 __builtin_abort ();
306 if (s.f_sx_ref_r () != xobj_fn)
307 __builtin_abort ();
308 if (s.f_sx_cref_r () != xobj_fn)
309 __builtin_abort ();
310 if (static_cast<S&&>(s).f_sx_rref_r () != xobj_fn)
311 __builtin_abort ();
312 if (static_cast<S&&>(s).f_sx_crref_r () != xobj_fn)
313 __builtin_abort ();
314 // if (s.f_sx_dv_r () != xobj_fn)
315 // __builtin_abort ();
316 // if (s.f_sx_dcref_r () != xobj_fn)
317 // __builtin_abort ();
318 // if (s.f_sx_dfwdref_r () != xobj_fn)
319 // __builtin_abort ();
320
321 // iobj/xobj
322
323 // The commented out cases are tested below as their correct behavior is
324 // unintuitive, see the note below for details.
325
326 if (s.f_ix_m0 () != iobj_fn)
327 __builtin_abort ();
328 // s.f_ix_m1
329 if (s.f_ix_c0 () != iobj_fn)
330 __builtin_abort ();
331 // s.f_ix_c1
332 if (s.f_xi_m0 () != xobj_fn)
333 __builtin_abort ();
334 // s.f_xi_m1
335 if (s.f_xi_c0 () != xobj_fn)
336 __builtin_abort ();
337 // s.f_xi_c1
338 if (s.f_ix_m0_refqual () != iobj_fn)
339 __builtin_abort ();
340 if (static_cast<S&&>(s).f_ix_m1_refqual () != iobj_fn)
341 __builtin_abort ();
342 if (s.f_ix_c0_refqual () != iobj_fn)
343 __builtin_abort ();
344 if (static_cast<S&&>(s).f_ix_c1_refqual () != iobj_fn)
345 __builtin_abort ();
346 if (s.f_xi_m0_refqual () != xobj_fn)
347 __builtin_abort ();
348 if (static_cast<S&&>(s).f_xi_m1_refqual () != xobj_fn)
349 __builtin_abort ();
350 if (s.f_xi_c0_refqual () != xobj_fn)
351 __builtin_abort ();
352 if (static_cast<S&&>(s).f_xi_c1_refqual () != xobj_fn)
353 __builtin_abort ();
354 if (s.f_ix_m0_r () != xobj_fn)
355 __builtin_abort ();
356 // s.f_ix_m1_r
357 if (s.f_ix_c0_r () != xobj_fn)
358 __builtin_abort ();
359 // s.f_ix_c1_r
360 if (s.f_xi_m0_r () != iobj_fn)
361 __builtin_abort ();
362 // s.f_xi_m1_r
363 if (s.f_xi_c0_r () != iobj_fn)
364 __builtin_abort ();
365 // s.f_xi_c1_r
366 if (s.f_ix_m0_refqual_r () != xobj_fn)
367 __builtin_abort ();
368 if (static_cast<S&&>(s).f_ix_m1_refqual_r () != xobj_fn)
369 __builtin_abort ();
370 if (s.f_ix_c0_refqual_r () != xobj_fn)
371 __builtin_abort ();
372 if (static_cast<S&&>(s).f_ix_c1_refqual_r () != xobj_fn)
373 __builtin_abort ();
374 if (s.f_xi_m0_refqual_r () != iobj_fn)
375 __builtin_abort ();
376 if (static_cast<S&&>(s).f_xi_m1_refqual_r () != iobj_fn)
377 __builtin_abort ();
378 if (s.f_xi_c0_refqual_r () != iobj_fn)
379 __builtin_abort ();
380 if (static_cast<S&&>(s).f_xi_c1_refqual_r () != iobj_fn)
381 __builtin_abort ();
382
383
384 /* Foreword: CWG2789 does not specifiy this correctly, it needs to be changed
385 to consider correspondence instead of "same type" or else the following
386 does not make sense. My implementation assumes correspondence should be
387 considered.
388
389 Note:
390 These cases are weird, the object argument correspond, but are not the same
391 type ([basic.scope.scope-3.1]), so we get this funny edge case where the
392 constraint stops them from being considered redeclarations, but isn't taken
393 into account for the lvalue case. You can't bind an lvalue to an rvalue
394 reference so the iobj member function is always taken regardless of which
395 overload is constrained.
396
397 [over.match.funcs.general-4]
398 For implicit object member functions, the type of the implicit object
399 parameter is
400 (4.1) “lvalue reference to cv X” for functions declared without a
401 ref-qualifier or with the & ref-qualifier
402
403 You would think that calling these functions with an rvalue would be the
404 same then, always taking the xobj member function. However, for backwards
405 compatibility reasons, an unqualified member function can be called on an
406 object that is an rvalue.
407
408 [over.match.funcs.general-5]
409 For implicit object member functions declared without a ref-qualifier, even
410 if the implicit object parameter is not const-qualified, an rvalue can be
411 bound to the parameter as long as in all other respects the argument can be
412 converted to the type of the implicit object parameter.
413
414 And finally, since the object parameters correspond ([basic.scope.scope-3.1])
415 the constraints are taken into account.
416
417 So in conclusion, calling these functions with an lvalue always resolves to
418 the iobj member function, and calling them with rvalues take the constraints
419 into account.
420
421 As wacky as this is, this is the correct behavior. */
422
423 // Always takes the iobj member function, can't bind an lvalue to an rvalue
424 // reference.
425 if (s.f_ix_m1 () != iobj_fn)
426 __builtin_abort ();
427 if (s.f_ix_c1 () != iobj_fn)
428 __builtin_abort ();
429 if (s.f_xi_m1 () != iobj_fn)
430 __builtin_abort ();
431 if (s.f_xi_c1 () != iobj_fn)
432 __builtin_abort ();
433
434 if (s.f_ix_m1_r () != iobj_fn)
435 __builtin_abort ();
436 if (s.f_ix_c1_r () != iobj_fn)
437 __builtin_abort ();
438 if (s.f_xi_m1_r () != iobj_fn)
439 __builtin_abort ();
440 if (s.f_xi_c1_r () != iobj_fn)
441 __builtin_abort ();
442
443 // Constraints are taken into account here, see note for more information.
444 if (static_cast<S&&>(s).f_ix_m1 () != iobj_fn)
445 __builtin_abort ();
446 if (static_cast<S&&>(s).f_ix_c1 () != iobj_fn)
447 __builtin_abort ();
448 if (static_cast<S&&>(s).f_xi_m1 () != xobj_fn)
449 __builtin_abort ();
450 if (static_cast<S&&>(s).f_xi_c1 () != xobj_fn)
451 __builtin_abort ();
452
453 if (static_cast<S&&>(s).f_ix_m1_r () != xobj_fn)
454 __builtin_abort ();
455 if (static_cast<S&&>(s).f_ix_c1_r () != xobj_fn)
456 __builtin_abort ();
457 if (static_cast<S&&>(s).f_xi_m1_r () != iobj_fn)
458 __builtin_abort ();
459 if (static_cast<S&&>(s).f_xi_c1_r () != iobj_fn)
460 __builtin_abort ();
461 }
462