1 // RUN: %clang_cc1 -verify -std=c++23 %s
7 int operator[](); // expected-note{{not viable: requires 0 arguments, but 1 was provided}}
11 S
{}[1]; // expected-error {{no viable overloaded operator[] for type 'S'}}
16 constexpr int operator[](int i
= 42) { return i
; } // expected-note {{not viable: allows at most single argument 'i'}}
18 static_assert(S
{}[] == 42);
19 static_assert(S
{}[1] == 1);
20 static_assert(S
{}[1, 2] == 1); // expected-error {{no viable overloaded operator[] for type 'S'}}
24 constexpr int operator[](auto... i
) { return (42 + ... + i
); }
29 static_assert(Variadic
{}[] == 42);
30 static_assert(Variadic
{}[1] == 43);
31 static_assert(Variadic
{}[1, 2] == 45);
36 constexpr int operator[]() { return 0; }
37 constexpr int operator[](int) { return 1; };
38 constexpr int operator[](int, int) { return 2; };
40 static_assert(S
{}[] == 0);
41 static_assert(S
{}[1] == 1);
42 static_assert(S
{}[1, 1] == 2);
47 constexpr int operator[]() { return 0; } // expected-note{{candidate function}}
48 constexpr int operator[](int = 0) { return 1; }; // expected-note{{candidate function}}
51 static_assert(S
{}[] == 0); // expected-error{{call to subscript operator of type 'S' is ambiguous}}
55 template <typename
... T
>
57 constexpr auto operator[](T
... arg
) { // expected-note {{candidate function not viable: requires 2 arguments, but 1 was provided}}
58 return (1 + ... + arg
);
62 static_assert(T1
<>{}[] == 1);
63 static_assert(T1
<int>{}[1] == 2);
64 static_assert(T1
<int, int>{}[1, 1] == 3);
65 static_assert(T1
<int, int>{}[1] == 3); // expected-error {{no viable overloaded operator[] for type 'T1<int, int>'}}
68 constexpr auto operator[](auto... arg
) {
69 return (1 + ... + arg
);
73 static_assert(T2
{}[] == 1);
74 static_assert(T2
{}[1] == 2);
75 static_assert(T2
{}[1, 1] == 3);
77 namespace test_packs
{
80 template<typename
... Ts
>
81 constexpr int operator[](Ts
... idx
) {
82 return (0 + ... + idx
);
87 constexpr int cxx_subscript() {
93 int cxx_subscript_unexpanded() {
95 return foo
[Is
]; // expected-error {{expression contains unexpanded parameter pack 'Is'}}
99 constexpr int c_array() {
100 int arr
[] = {1, 2, 3};
101 return arr
[Is
...]; // expected-error 2{{type 'int[3]' does not provide a subscript operator}}
105 int c_array_unexpanded() {
106 int arr
[] = {1, 2, 3};
107 return arr
[Is
]; // expected-error {{expression contains unexpanded parameter pack 'Is'}}
111 static_assert(cxx_subscript
<1, 2, 3>() == 6);
112 static_assert(c_array
<1>() == 2);
114 c_array
<>(); // expected-note {{in instantiation}}
116 c_array
<1, 2>(); // expected-note {{in instantiation}}