brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · c734b82 Raw
120 lines · cpp
1// RUN: %clang_cc1 -verify -std=c++23 %s2 3namespace N {4 5void 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 14void 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 23struct Variadic {24  constexpr int operator[](auto... i) { return (42 + ... + i); }25};26 27void variadic() {28 29  static_assert(Variadic{}[] == 42);30  static_assert(Variadic{}[1] == 43);31  static_assert(Variadic{}[1, 2] == 45);32}33 34void 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 45void 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 N54 55template <typename... T>56struct T1 {57  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 62static_assert(T1<>{}[] == 1);63static_assert(T1<int>{}[1] == 2);64static_assert(T1<int, int>{}[1, 1] == 3);65static_assert(T1<int, int>{}[1] == 3); // expected-error {{no viable overloaded operator[] for type 'T1<int, int>'}}66 67struct T2 {68  constexpr auto operator[](auto... arg) {69    return (1 + ... + arg);70  }71};72 73static_assert(T2{}[] == 1);74static_assert(T2{}[1] == 2);75static_assert(T2{}[1, 1] == 3);76 77namespace test_packs {78 79struct foo_t {80template<typename... Ts>81constexpr int operator[](Ts... idx) {82    return (0 + ... + idx);83}84};85 86template<int... Is>87constexpr int cxx_subscript() {88  foo_t foo;89  return foo[Is...];90}91 92template<int... Is>93int cxx_subscript_unexpanded() {94  foo_t foo;95  return foo[Is]; // expected-error {{expression contains unexpanded parameter pack 'Is'}}96}97 98template<int... Is>99constexpr 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 104template<int... Is>105int c_array_unexpanded() {106  int arr[] = {1, 2, 3};107  return arr[Is]; // expected-error {{expression contains unexpanded parameter pack 'Is'}}108}109 110void 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