| 1 |
// P2662R3 - Pack Indexing |
| 2 |
// PR c++/113798 |
| 3 |
// { dg-do compile { target c++26 } } |
| 4 |
// Test invalid cases. |
| 5 |
|
| 6 |
template<int I, typename... Ts> |
| 7 |
using Type = Typo...[I]; // { dg-error "does not name a type" } |
| 8 |
|
| 9 |
template<int I, auto... Ts> |
| 10 |
constexpr auto Var = Typo...[I]; // { dg-error "no parameter packs" } |
| 11 |
|
| 12 |
template<typename... Ts> |
| 13 |
void foo(Ts...[]); // { dg-error "pack index missing" } |
| 14 |
|
| 15 |
template <typename... Ts> |
| 16 |
void f(Ts...[1]); |
| 17 |
|
| 18 |
template<int... N> |
| 19 |
int f2 (X... [N]); // { dg-error "contains no parameter packs" } |
| 20 |
|
| 21 |
struct X; |
| 22 |
|
| 23 |
template<typename T> |
| 24 |
struct TX; |
| 25 |
|
| 26 |
template <typename T, auto V, template<typename> typename Tp> |
| 27 |
void |
| 28 |
bad (int i) |
| 29 |
{ |
| 30 |
i...[0]; // { dg-error "no parameter packs" } |
| 31 |
V...[0]; // { dg-error "no parameter packs" } |
| 32 |
X...[0] x; // { dg-error "no parameter packs" } |
| 33 |
T...[0] t; // { dg-error "no parameter packs" } |
| 34 |
Tp...[0] tp; // { dg-error "expected" } |
| 35 |
|
| 36 |
X...[0] xarr[1]; // { dg-error "no parameter packs" } |
| 37 |
T...[0] tarr[1]; // { dg-error "no parameter packs" } |
| 38 |
Tp...[0] tparr[1]; // { dg-error "expected" } |
| 39 |
} |
| 40 |
|
| 41 |
template<int N> |
| 42 |
int |
| 43 |
getT (auto... Ts) |
| 44 |
{ |
| 45 |
return Ts...[N]; // { dg-error "cannot index an empty pack" } |
| 46 |
} |
| 47 |
|
| 48 |
template<int N> |
| 49 |
int |
| 50 |
getT2 (auto... Ts) |
| 51 |
{ |
| 52 |
return Ts...[N]; // { dg-error "pack index is out of range" } |
| 53 |
} |
| 54 |
|
| 55 |
template<auto N, typename... Ts> |
| 56 |
void |
| 57 |
badtype () |
| 58 |
{ |
| 59 |
Ts...[N] t; // { dg-error "cannot index an empty pack" } |
| 60 |
} |
| 61 |
|
| 62 |
template<auto N, typename... Ts> |
| 63 |
void |
| 64 |
badtype2 () |
| 65 |
{ |
| 66 |
Ts...[N] t; // { dg-error "pack index is out of range" } |
| 67 |
} |
| 68 |
|
| 69 |
template<auto N, typename... Ts> |
| 70 |
void |
| 71 |
badtype3 () |
| 72 |
{ |
| 73 |
Ts...[N] t; // { dg-error "cannot index an empty pack" } |
| 74 |
} |
| 75 |
|
| 76 |
template<auto N, typename... Ts> |
| 77 |
void |
| 78 |
badtype4 () |
| 79 |
{ |
| 80 |
Ts...[N] t; // { dg-error "narrowing conversion of '-1' from 'int' to" } |
| 81 |
} |
| 82 |
|
| 83 |
int nonconst () { return 42; } |
| 84 |
|
| 85 |
template<typename... Ts> |
| 86 |
void |
| 87 |
badindex () |
| 88 |
{ |
| 89 |
Ts...[nonconst ()] t; // { dg-error "pack index is not an integral constant" } |
| 90 |
// { dg-error "non-.constexpr. function" "" { target *-*-* } .-1 } |
| 91 |
} |
| 92 |
|
| 93 |
template<typename... Ts> |
| 94 |
struct broken { |
| 95 |
Ts...1; // { dg-error "expected" } |
| 96 |
Ts...[; // { dg-error "invalid" } |
| 97 |
Ts...[1; // { dg-error "invalid" } |
| 98 |
Ts...[]; // { dg-error "pack index missing" } |
| 99 |
|
| 100 |
void foo (auto...Vs) { |
| 101 |
decltype(Vs...[1]) d1 = Vs...[]; // { dg-error "pack index missing" } |
| 102 |
decltype(Vs...[1]) d2 = Vs...[; // { dg-error "expected" } |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
int main() |
| 107 |
{ |
| 108 |
// void f<int, double>(int [1], double [1]) |
| 109 |
f<int, double>(nullptr, nullptr); // { dg-error "no matching function" } |
| 110 |
bad<int, 0, TX>(42); |
| 111 |
|
| 112 |
getT<0>(); // { dg-message "required from here" } |
| 113 |
getT<1>(); // { dg-message "required from here" } |
| 114 |
getT2<-1>(1); // { dg-message "required from here" } |
| 115 |
|
| 116 |
badtype<0>(); // { dg-message "required from here" } |
| 117 |
badtype2<1, int>(); // { dg-message "required from here" } |
| 118 |
badtype3<-1>(); // { dg-message "required from here" } |
| 119 |
badtype4<-1, int>(); // { dg-message "required from here" } |
| 120 |
|
| 121 |
badindex<int, int, int>(); |
| 122 |
|
| 123 |
bool b = nothere...[0]; // { dg-error "no parameter packs" } |
| 124 |
using E = nothere...[0]; // { dg-error "does not name a type" } |
| 125 |
} |