[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / cxx2b-overloaded-operator.cpp
blobc734b82987083b0bf672168df69ece980e25dd2a
1 // RUN: %clang_cc1 -verify -std=c++23 %s
3 namespace N {
5 void empty() {
6 struct S {
7 int operator[](); // expected-note{{not viable: requires 0 arguments, but 1 was provided}}
8 };
10 S{}[];
11 S{}[1]; // expected-error {{no viable overloaded operator[] for type 'S'}}
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'}}
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'}}
23 struct Variadic {
24 constexpr int operator[](auto... i) { return (42 + ... + i); }
27 void variadic() {
29 static_assert(Variadic{}[] == 42);
30 static_assert(Variadic{}[1] == 43);
31 static_assert(Variadic{}[1, 2] == 45);
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; };
40 static_assert(S{}[] == 0);
41 static_assert(S{}[1] == 1);
42 static_assert(S{}[1, 1] == 2);
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}}
51 static_assert(S{}[] == 0); // expected-error{{call to subscript operator of type 'S' is ambiguous}}
53 } // namespace N
55 template <typename... T>
56 struct T1 {
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>'}}
67 struct T2 {
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 {
79 struct foo_t {
80 template<typename... Ts>
81 constexpr int operator[](Ts... idx) {
82 return (0 + ... + idx);
86 template<int... Is>
87 constexpr int cxx_subscript() {
88 foo_t foo;
89 return foo[Is...];
92 template<int... Is>
93 int cxx_subscript_unexpanded() {
94 foo_t foo;
95 return foo[Is]; // expected-error {{expression contains unexpanded parameter pack 'Is'}}
98 template<int... 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}}
104 template<int... Is>
105 int c_array_unexpanded() {
106 int arr[] = {1, 2, 3};
107 return arr[Is]; // expected-error {{expression contains unexpanded parameter pack 'Is'}}
110 void test() {
111 static_assert(cxx_subscript<1, 2, 3>() == 6);
112 static_assert(c_array<1>() == 2);
114 c_array<>(); // expected-note {{in instantiation}}
115 c_array<1>();
116 c_array<1, 2>(); // expected-note {{in instantiation}}