File: cxx2c-pack-indexing.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (233 lines) | stat: -rw-r--r-- 6,696 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// RUN: %clang_cc1 -std=c++2c -verify %s

struct NotAPack;
template <typename T, auto V, template<typename> typename Tp>
void not_pack() {
    int i = 0;
    i...[0]; // expected-error {{i does not refer to the name of a parameter pack}}
    V...[0]; // expected-error {{V does not refer to the name of a parameter pack}}
    NotAPack...[0] a; // expected-error{{'NotAPack' does not refer to the name of a parameter pack}}
    T...[0] b;   // expected-error{{'T' does not refer to the name of a parameter pack}}
    Tp...[0] c; // expected-error{{'Tp' does not refer to the name of a parameter pack}}
}

template <typename T, auto V, template<typename> typename Tp>
void not_pack_arrays() {
    NotAPack...[0] a[1]; // expected-error{{'NotAPack' does not refer to the name of a parameter pack}}
    T...[0] b[1];   // expected-error{{'T' does not refer to the name of a parameter pack}}
    Tp...[0] c[1]; // expected-error{{'Tp' does not refer to the name of a parameter pack}}
}

template <typename T>
struct TTP;

void test_errors() {
    not_pack<int, 0, TTP>();
    not_pack_arrays<int, 0, TTP>();
}

namespace invalid_indexes {

int non_constant_index(); // expected-note 2{{declared here}}

template <int idx>
int params(auto... p) {
    return p...[idx]; // #error-param-size
}

template <auto N, typename...T>
int test_types() {
    T...[N] a; // #error-type-size
}

void test() {
    params<0>();   // expected-note{{here}} \
                   // expected-error@#error-param-size {{invalid index 0 for pack p of size 0}}
    params<1>(0);  // expected-note{{here}} \
                   // expected-error@#error-param-size {{invalid index 1 for pack p of size 1}}
    params<-1>(0); // expected-note{{here}} \
                   // expected-error@#error-param-size {{invalid index -1 for pack p of size 1}}

    test_types<-1>(); //expected-note {{in instantiation}} \
                      // expected-error@#error-type-size {{invalid index -1 for pack 'T' of size 0}}
    test_types<-1, int>(); //expected-note {{in instantiation}} \
                      // expected-error@#error-type-size {{invalid index -1 for pack 'T' of size 1}}
    test_types<0>(); //expected-note {{in instantiation}} \
                    // expected-error@#error-type-size {{invalid index 0 for pack 'T' of size 0}}
    test_types<1, int>(); //expected-note {{in instantiation}}  \
                         // expected-error@#error-type-size {{invalid index 1 for pack 'T' of size 1}}
}

void invalid_indexes(auto... p) {
    p...[non_constant_index()]; // expected-error {{array size is not a constant expression}}\
                                // expected-note {{cannot be used in a constant expression}}

    const char* no_index = "";
    p...[no_index]; // expected-error {{value of type 'const char *' is not implicitly convertible}}
}

void invalid_index_types() {
    []<typename... T> {
        T...[non_constant_index()] a;  // expected-error {{array size is not a constant expression}}\
                                       // expected-note {{cannot be used in a constant expression}}
    }(); //expected-note {{in instantiation}}
}

}

template <typename T, typename U>
constexpr bool is_same = false;

template <typename T>
constexpr bool is_same<T, T> = true;

template <typename T>
constexpr bool f(auto&&... p) {
    return is_same<T, decltype(p...[0])>;
}

void g() {
    int a = 0;
    const int b = 0;
    static_assert(f<int&&>(0));
    static_assert(f<int&>(a));
    static_assert(f<const int&>(b));
}

template <auto... p>
struct check_ice {
    enum e {
        x = p...[0]
    };
};

static_assert(check_ice<42>::x == 42);

struct S{};
template <auto... p>
constexpr auto constant_initializer = p...[0];
constexpr auto InitOk = constant_initializer<S{}>;

consteval int evaluate(auto... p) {
    return p...[0];
}
constexpr int x = evaluate(42, S{});
static_assert(x == 42);


namespace splice {
template <auto ... Is>
struct IL{};

template <typename ... Ts>
struct TL{};

template <typename Tl, typename Il>
struct SpliceImpl;

template <typename ... Ts, auto ...Is>
struct SpliceImpl<TL<Ts...>, IL<Is...>>{
    using type = TL<Ts...[Is]...>;
};

template <typename Tl, typename Il>
using Splice = typename SpliceImpl<Tl, Il>::type;
using type = Splice<TL<char, short, long, double>, IL<1, 2>>;
static_assert(is_same<type, TL<short, long>>);
}


namespace GH81697 {

template<class... Ts> struct tuple {
    int __x0;
};

template<auto I, class... Ts>
Ts...[I]& get(tuple<Ts...>& t) {
  return t.__x0;
}

void f() {
  tuple<int> x;
  get<0>(x);
}

}

namespace GH88929 {
    bool b = a...[0];  // expected-error {{use of undeclared identifier 'a'}}
    using E = P...[0]; // expected-error {{unknown type name 'P'}} \
                       // expected-error {{expected ';' after alias declaration}}
}

namespace GH88925 {
template <typename...> struct S {};

template <auto...> struct W {};

template <int...> struct sequence {};

template <typename... args, int... indices> auto f(sequence<indices...>) {
  return S<args...[indices]...>(); // #use
}

template <auto... args, int... indices> auto g(sequence<indices...>) {
  return W<args...[indices]...>(); // #nttp-use
}

void h() {
  static_assert(__is_same(decltype(f<int>(sequence<0, 0>())), S<int, int>));
  static_assert(__is_same(decltype(f<int, long>(sequence<0, 0>())), S<int, int>));
  static_assert(__is_same(decltype(f<int, long>(sequence<0, 1>())), S<int, long>));
  f<int, long>(sequence<3>());
  // expected-error@#use {{invalid index 3 for pack 'args' of size 2}}}
  // expected-note-re@-2 {{function template specialization '{{.*}}' requested here}}

  struct foo {};
  struct bar {};
  struct baz {};

  static_assert(__is_same(decltype(g<foo{}, bar{}, baz{}>(sequence<0, 2, 1>())), W<foo{}, baz{}, bar{}>));
  g<foo{}>(sequence<4>());
  // expected-error@#nttp-use {{invalid index 4 for pack args of size 1}}
  // expected-note-re@-2 {{function template specialization '{{.*}}' requested here}}
}
}

namespace GH91885 {

void test(auto...args){
    [&]<int idx>(){
        using R = decltype( args...[idx] ) ;
    }.template operator()<0>();
}

template<int... args>
void test2(){
  [&]<int idx>(){
    using R = decltype( args...[idx] ) ; // #test2-R
  }.template operator()<0>(); // #test2-call
}

void f( ) {
  test(1);
  test2<1>();
  test2();
  // expected-error@#test2-R {{invalid index 0 for pack args of size 0}}
  // expected-note@#test2-call {{requested here}}
  // expected-note@-3 {{requested here}}
}


}

namespace std {
struct type_info {
  const char *name;
};
} // namespace std

namespace GH93650 {
auto func(auto... inputArgs) { return typeid(inputArgs...[0]); }
} // namespace GH93650