Debian Codegraph @

gcc/testsuite/g++.dg/cpp23/subscript2.C

raw
1 // P2128R6
2 // { dg-do compile }
3 // { dg-options "-std=c++23" }
4
5 struct S
6 {
7 S () : a {} {};
8 int &operator[] () { return a[0]; }
9 int &operator[] (int x) { return a[x]; }
10 int &operator[] (int x, long y) { return a[x + y * 8]; }
11 int a[64];
12 };
13
14 struct T
15 {
16 operator int () { return 42; };
17 };
18
19 int buf[64];
20
21 struct U
22 {
23 operator int * () { return buf; }
24 };
25
26 struct V
27 {
28 V () : a {} {};
29 V (int x, int y, int z) : a {x, y, z} {};
30 int &operator[] () { return a[0]; } // { dg-message "candidate" }
31 int &operator[] (int x, long y) { return a[x + y * 8]; } // { dg-message "candidate" }
32 int a[64];
33 };
34
35 void
36 foo ()
37 {
38 S s;
39 T t;
40 U u;
41 V v;
42 auto &a = buf[]; // { dg-error "built-in subscript operator without expression list" }
43 auto &b = buf[1, 2]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
44 auto &c = s[1, 2, 3]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
45 auto &d = v[1]; // { dg-error "no match for 'operator\\\[\\\]' in 'v\\\[1\\\]' \\\(operand types are 'V' and 'int'\\\)" }
46 auto &e = v[1, 2, 3]; // { dg-error "no match for call to 'V::operator\\\[\\\] \\\(int, int, int\\\)'" }
47 auto &f = t[42, u]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
48 auto &g = u[42, t]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
49 auto &h = buf[42, 2.5]; // { dg-warning "top-level comma expression in array subscript changed meaning in" }
50 // { dg-error "invalid types \[^\n\r]* for array subscript" "" { target *-*-* } .-1 }
51 }