File: cxx2c-pack-indexing.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.8-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,388 kB
  • sloc: cpp: 7,438,767; ansic: 1,393,871; asm: 1,012,926; python: 241,728; f90: 86,635; objc: 75,411; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (346 lines) | stat: -rw-r--r-- 9,155 bytes parent folder | download
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// 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


namespace GH105900 {

template <typename... opts>
struct types  {
    template <unsigned idx>
    static constexpr __SIZE_TYPE__ get_index() { return idx; }

    template <unsigned s>
    static auto x() -> opts...[get_index<s>()] {}
};

template <auto... opts>
struct vars  {
    template <unsigned idx>
    static constexpr __SIZE_TYPE__ get_index() { return idx; }

    template <unsigned s>
    static auto x() -> decltype(opts...[get_index<s>()]) {return 0;}
};

void f() {
    types<void>::x<0>();
    vars<0>::x<0>();
}

} // namespace GH105900

namespace GH105903 {

template <typename... opts> struct temp {
  template <unsigned s> static auto x() -> opts... [s] {} // expected-note {{invalid index 0 for pack 'opts' of size 0}}
};

void f() {
  temp<>::x<0>(); // expected-error {{no matching}}
}

} // namespace GH105903

namespace GH116105 {

template <unsigned long Np, class... Ts> using pack_type = Ts...[Np];

template <unsigned long Np, auto... Ts> using pack_expr = decltype(Ts...[Np]);

template <class...> struct types;

template <class, long... Is> struct indices;

template <class> struct repack;

template <long... Idx> struct repack<indices<long, Idx...>> {
  template <class... Ts>
  using pack_type_alias = types<pack_type<Idx, Ts...>...>;

  template <class... Ts>
  using pack_expr_alias = types<pack_expr<Idx, Ts{}...>...>;
};

template <class... Args> struct mdispatch_ {
  using Idx = __make_integer_seq<indices, long, sizeof...(Args)>;

  static_assert(__is_same(
      typename repack<Idx>::template pack_type_alias<Args...>, types<Args...>));

  static_assert(__is_same(
      typename repack<Idx>::template pack_expr_alias<Args...>, types<Args...>));
};

mdispatch_<int, int> d;

} // namespace GH116105

namespace GH121242 {
    // Non-dependent type pack access
    template <int...x>
    int y = x...[0];

    struct X {};

    template <X...x>
    X z = x...[0];

    void foo() {
        (void)y<0>;
        (void)z<X{}>;
    }
} // namespace GH121242

namespace GH123033 {
  template <class... Types>
  requires __is_same_as(Types...[0], int)
  void print(double d);

  template <class... Types>
  requires  __is_same_as(Types...[0], int)
  void print(double d);

  template <class... Types>
  Types...[0] convert(double d);

  template <class... Types>
  Types...[0] convert(double d) {
      return static_cast<Types...[0]>(d);
  }

  void f() {
      print<int, int>(12.34);
      convert<int, int>(12.34);
  }
}