| 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 |
template <int N> |
| 30 |
void |
| 31 |
foo () |
| 32 |
{ |
| 33 |
static_assert (S ()[1] == 0); |
| 34 |
static_assert (S (1, 2, 42)[2] == 42); |
| 35 |
static_assert (S ()[3, 4] == 0); |
| 36 |
static_assert (S (1, 43, 2)[1, 0] == 43); |
| 37 |
static_assert (S ()[] == 0); |
| 38 |
static_assert (S (44, 1, 2)[] == 44); |
| 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 |
} |
| 56 |
|
| 57 |
template <typename V, typename W, typename X> |
| 58 |
void |
| 59 |
bar () |
| 60 |
{ |
| 61 |
static_assert (V ()[1] == 0); |
| 62 |
static_assert (V (1, 2, 42)[2] == 42); |
| 63 |
static_assert (V ()[3, 4] == 0); |
| 64 |
static_assert (V (1, 43, 2)[1, 0] == 43); |
| 65 |
static_assert (V ()[] == 0); |
| 66 |
static_assert (V (44, 1, 2)[] == 44); |
| 67 |
V s; |
| 68 |
for (int i = 0; i < 64; i++) |
| 69 |
s.a[i] = 64 - i; |
| 70 |
if (s[] != 64 || s[3] != 61 || s[4, 5] != 20) |
| 71 |
abort (); |
| 72 |
s[]++; |
| 73 |
s[42]++; |
| 74 |
++s[3, 2]; |
| 75 |
if (s.a[0] != 65 || s.a[42] != 23 || s.a[19] != 46) |
| 76 |
abort (); |
| 77 |
W t; |
| 78 |
X u; |
| 79 |
if (&u[t] != &buf[42]) |
| 80 |
abort (); |
| 81 |
if (&t[u] != &buf[42]) |
| 82 |
abort (); |
| 83 |
} |
| 84 |
|
| 85 |
int |
| 86 |
main () |
| 87 |
{ |
| 88 |
foo <0> (); |
| 89 |
bar <S, T, U> (); |
| 90 |
} |