Debian Codegraph @

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

raw
1 // P2128R6
2 // { dg-do run }
3 // { dg-options "-std=c++23" }
4
5 extern "C" void abort ();
6
7 struct S
8 {
9 constexpr S () : a {} {};
10 constexpr S (int x, int y, int z) : a {x, y, z} {};
11 constexpr int &operator[] () { return a[0]; }
12 constexpr int &operator[] (int x) { return a[x]; }
13 constexpr int &operator[] (int x, long y) { return a[x + y * 8]; }
14 int a[64];
15 };
16
17 struct T
18 {
19 operator int () { return 42; };
20 };
21
22 int buf[64];
23
24 struct U
25 {
26 operator int * () { return buf; }
27 };
28
29 static_assert (S ()[1] == 0);
30 static_assert (S (1, 2, 42)[2] == 42);
31 static_assert (S ()[3, 4] == 0);
32 static_assert (S (1, 43, 2)[1, 0] == 43);
33 static_assert (S ()[] == 0);
34 static_assert (S (44, 1, 2)[] == 44);
35
36 int
37 main ()
38 {
39 S s;
40 for (int i = 0; i < 64; i++)
41 s.a[i] = 64 - i;
42 if (s[] != 64 || s[3] != 61 || s[4, 5] != 20)
43 abort ();
44 s[]++;
45 s[42]++;
46 ++s[3, 2];
47 if (s.a[0] != 65 || s.a[42] != 23 || s.a[19] != 46)
48 abort ();
49 T t;
50 U u;
51 if (&u[t] != &buf[42])
52 abort ();
53 if (&t[u] != &buf[42])
54 abort ();
55 }