| 1 |
// P2662R3 - Pack Indexing |
| 2 |
// PR c++/113798 |
| 3 |
// { dg-do compile { target c++26 } } |
| 4 |
// From LLVM's cxx2c-pack-indexing.cpp. |
| 5 |
|
| 6 |
template <class, class> |
| 7 |
constexpr bool is_same = false; |
| 8 |
template <class T> |
| 9 |
constexpr bool is_same<T, T> = true; |
| 10 |
|
| 11 |
template <typename T> |
| 12 |
constexpr bool |
| 13 |
f (auto&&... p) |
| 14 |
{ |
| 15 |
return is_same<T, decltype(p...[0])>; |
| 16 |
} |
| 17 |
|
| 18 |
void |
| 19 |
g () |
| 20 |
{ |
| 21 |
int a = 0; |
| 22 |
const int b = 0; |
| 23 |
static_assert(f<int&&>(0)); |
| 24 |
static_assert(f<int&>(a)); |
| 25 |
static_assert(f<const int&>(b)); |
| 26 |
} |
| 27 |
|
| 28 |
template<auto... p> |
| 29 |
struct A { |
| 30 |
enum E { |
| 31 |
x = p...[0] |
| 32 |
}; |
| 33 |
}; |
| 34 |
static_assert(A<42>::x == 42); |
| 35 |
|
| 36 |
struct S { }; |
| 37 |
template<auto... p> |
| 38 |
constexpr auto constant_initializer = p...[0]; |
| 39 |
constexpr auto InitOk = constant_initializer<S{}>; |
| 40 |
|
| 41 |
consteval int evaluate(auto... p) { |
| 42 |
return p...[0]; |
| 43 |
} |
| 44 |
constexpr int x = evaluate(42, S{}); |
| 45 |
static_assert(x == 42); |
| 46 |
|
| 47 |
template <auto... Is> |
| 48 |
struct IL{}; |
| 49 |
|
| 50 |
template <typename... Ts> |
| 51 |
struct TL{}; |
| 52 |
|
| 53 |
template <typename Tl, typename Il> |
| 54 |
struct SpliceImpl; |
| 55 |
|
| 56 |
template <typename... Ts, auto... Is> |
| 57 |
struct SpliceImpl<TL<Ts...>, IL<Is...>> |
| 58 |
{ |
| 59 |
using type = TL<Ts...[Is]...>; |
| 60 |
}; |
| 61 |
|
| 62 |
template <typename Tl, typename Il> |
| 63 |
using Splice = typename SpliceImpl<Tl, Il>::type; |
| 64 |
using type = Splice<TL<char, short, long, double>, IL<1, 2>>; |
| 65 |
static_assert(is_same<type, TL<short, long>>); |