xref: /llvm-project/clang/test/SemaCXX/cxx2b-overloaded-operator.cpp (revision ba15d186e5cef2620d562c6c9d9a6d570382cd0a)
1 // RUN: %clang_cc1 -verify -std=c++23 %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 
77 namespace test_packs {
78 
79 struct foo_t {
80 template<typename... Ts>
operator []test_packs::foo_t81 constexpr int operator[](Ts... idx) {
82     return (0 + ... + idx);
83 }
84 };
85 
86 template<int... Is>
cxx_subscript()87 constexpr int cxx_subscript() {
88   foo_t foo;
89   return foo[Is...];
90 }
91 
92 template<int... Is>
cxx_subscript_unexpanded()93 int cxx_subscript_unexpanded() {
94   foo_t foo;
95   return foo[Is]; // expected-error {{expression contains unexpanded parameter pack 'Is'}}
96 }
97 
98 template<int... Is>
c_array()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}}
102 }
103 
104 template<int... Is>
c_array_unexpanded()105 int c_array_unexpanded() {
106   int arr[] = {1, 2, 3};
107   return arr[Is]; // expected-error {{expression contains unexpanded parameter pack 'Is'}}
108 }
109 
test()110 void test() {
111   static_assert(cxx_subscript<1, 2, 3>() == 6);
112   static_assert(c_array<1>() == 2);
113 
114   c_array<>(); // expected-note {{in instantiation}}
115   c_array<1>();
116   c_array<1, 2>(); // expected-note {{in instantiation}}
117 }
118 
119 }
120