1 // RUN: %clang_cc1 -verify -std=c++2b %s
2 
3 namespace N {
4 
empty()5 void empty() {
6   struct S {
7     int operator[](); // expected-note{{not viable: requires 0 arguments, but 1 was provided}}
8   };
9 
10   S{}[];
11   S{}[1]; // expected-error {{no viable overloaded operator[] for type 'S'}}
12 }
13 
default_var()14 void default_var() {
15   struct S {
16     constexpr int operator[](int i = 42) { return i; } // expected-note {{not viable: allows at most single argument 'i'}}
17   };
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'}}
21 }
22 
23 struct Variadic {
operator []N::Variadic24   constexpr int operator[](auto... i) { return (42 + ... + i); }
25 };
26 
variadic()27 void variadic() {
28 
29   static_assert(Variadic{}[] == 42);
30   static_assert(Variadic{}[1] == 43);
31   static_assert(Variadic{}[1, 2] == 45);
32 }
33 
multiple()34 void multiple() {
35   struct S {
36     constexpr int operator[]() { return 0; }
37     constexpr int operator[](int) { return 1; };
38     constexpr int operator[](int, int) { return 2; };
39   };
40   static_assert(S{}[] == 0);
41   static_assert(S{}[1] == 1);
42   static_assert(S{}[1, 1] == 2);
43 }
44 
ambiguous()45 void ambiguous() {
46   struct S {
47     constexpr int operator[]() { return 0; }         // expected-note{{candidate function}}
48     constexpr int operator[](int = 0) { return 1; }; // expected-note{{candidate function}}
49   };
50 
51   static_assert(S{}[] == 0); // expected-error{{call to subscript operator of type 'S' is ambiguous}}
52 }
53 } // namespace N
54 
55 template <typename... T>
56 struct T1 {
operator []T157   constexpr auto operator[](T... arg) { // expected-note {{candidate function not viable: requires 2 arguments, but 1 was provided}}
58     return (1 + ... + arg);
59   }
60 };
61 
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>'}}
66 
67 struct T2 {
operator []T268   constexpr auto operator[](auto... arg) {
69     return (1 + ... + arg);
70   }
71 };
72 
73 static_assert(T2{}[] == 1);
74 static_assert(T2{}[1] == 2);
75 static_assert(T2{}[1, 1] == 3);
76