aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp23/subscript3.C
blob: 2d735e4e0e0ca347b68df680bc9f995c3dda5e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// P2128R6
// { dg-do run }
// { dg-options "-std=c++23" }

extern "C" void abort ();

struct S
{
  constexpr S () : a {} {};
  constexpr S (int x, int y, int z) : a {x, y, z} {};
  constexpr int &operator[] () { return a[0]; }
  constexpr int &operator[] (int x) { return a[x]; }
  constexpr int &operator[] (int x, long y) { return a[x + y * 8]; }
  int a[64];
};

struct T
{
  operator int () { return 42; };
};

int buf[64];

struct U
{
  operator int * () { return buf; }
};

template <int N>
void
foo ()
{
  static_assert (S ()[1] == 0);
  static_assert (S (1, 2, 42)[2] == 42);
  static_assert (S ()[3, 4] == 0);
  static_assert (S (1, 43, 2)[1, 0] == 43);
  static_assert (S ()[] == 0);
  static_assert (S (44, 1, 2)[] == 44);
  S s;
  for (int i = 0; i < 64; i++)
    s.a[i] = 64 - i;
  if (s[] != 64 || s[3] != 61 || s[4, 5] != 20)
    abort ();
  s[]++;
  s[42]++;
  ++s[3, 2];
  if (s.a[0] != 65 || s.a[42] != 23 || s.a[19] != 46)
    abort ();
  T t;
  U u;
  if (&u[t] != &buf[42])
    abort ();
  if (&t[u] != &buf[42])
    abort ();
}

template <typename V, typename W, typename X>
void
bar ()
{
  static_assert (V ()[1] == 0);
  static_assert (V (1, 2, 42)[2] == 42);
  static_assert (V ()[3, 4] == 0);
  static_assert (V (1, 43, 2)[1, 0] == 43);
  static_assert (V ()[] == 0);
  static_assert (V (44, 1, 2)[] == 44);
  V s;
  for (int i = 0; i < 64; i++)
    s.a[i] = 64 - i;
  if (s[] != 64 || s[3] != 61 || s[4, 5] != 20)
    abort ();
  s[]++;
  s[42]++;
  ++s[3, 2];
  if (s.a[0] != 65 || s.a[42] != 23 || s.a[19] != 46)
    abort ();
  W t;
  X u;
  if (&u[t] != &buf[42])
    abort ();
  if (&t[u] != &buf[42])
    abort ();
}

int
main ()
{
  foo <0> ();
  bar <S, T, U> ();
}