How does C++26 pack indexing with Ts...[I] and values...[I] replace the older ways of reaching the Nth element of a pack?
answer
- subscript a pack directly
- a type form and an expression form
- index is a constant below sizeof...
- tuple_element or recursion before
basics
~20 sC++26 lets you subscript a pack: Ts...[I] names the Ith type and values...[I] the Ith value, with I a constant expression below sizeof.... Earlier code needed recursive helper templates or std::tuple_element_t and std::get on a tuple.
solid answer
~40 sC++26 adds a **pack-index-specifier**, `Ts...[I]`, naming the type at position `I`, and a **pack-index-expression**, `values...[I]`, naming the element at position `I`. `I` must be a converted constant expression of type `std::size_t` with `0 <= I < sizeof...(pack)`; Clang reports `invalid index 3 for pack 'v' of size 3` otherwise, including for any index into an empty pack. So `last(values...)` is simply `values...[sizeof...(Ts) - 1]`, and `Record<Ts...>` can expose `template <std::size_t I> using field_type = Ts...[I];`. Before C++26 the same needed a recursive type-at template that peeled one element per level, or a detour through `std::tuple_element_t<I, std::tuple<Ts...>>` and `std::get<I>(std::forward_as_tuple(values...))`. Pack indexing compiles faster and reads directly. One break: `T...[n]` used to declare a pack of unnamed array parameters, as in `void f(T... [1])`; C++26 reads it as pack indexing.
code
cpp · 21 lines#include <cstdio>
#include <string>
#include <type_traits>
template <typename... Ts>
struct Record {
template <std::size_t I>
using field_type = Ts...[I]; // C++26 pack-index-specifier
};
template <std::size_t I, typename... Ts>
auto field(Ts... values) { return values...[I]; } // C++26 pack-index-expression
template <typename... Ts>
auto last(Ts... values) { return values...[sizeof...(Ts) - 1]; }
int main() {
using R = Record<int, double, std::string>;
static_assert(std::is_same_v<R::field_type<2>, std::string>);
std::printf("%.1f %s\n", field<1>(7, 0.5, "eu-1"), last(7, 0.5, "eu-1"));
}go deeper
Recognize Ts...[0] as C++26 syntax for the first type in a pack.
Use both forms, type and value, and know the index must be a compile-time constant within the pack's size.
Replace recursive and tuple-based helpers with pack indexing where the toolchain allows, and know the old T...[n] parameter syntax it breaks.
Plan adoption behind an alias so code builds on compilers without C++26 support while new toolchains use the cheaper, clearer form.
## The problem pack indexing solves A parameter pack has no subscript before C++26. Expansion and folds apply a pattern to every element, but reaching one element by position — the first, the last, the Ith — needed indirection: - **Recursive templates** that peel one element per level until the index reaches zero. - **Tuple detours**: `std::tuple_element_t<I, std::tuple<Ts...>>` for the type and `std::get<I>(std::forward_as_tuple(values...))` for the value. These work but instantiate a tuple type just to index it, and the tuple machinery belongs to another part of the library. - **Compiler built-ins**: libc++ itself reaches the Ith type of a pack through Clang's `__type_pack_element` inside `<variant>` and its tuple headers — a vendor extension, not portable user code. ## The C++26 syntax C++26 adds two forms, both written with the ellipsis followed by a subscript: - **Pack-index-specifier** — a type: `Ts...[I]`. - **Pack-index-expression** — an expression: `values...[I]`. ```cpp template <typename... Ts> struct Record { template <std::size_t I> using field_type = Ts...[I]; }; template <std::size_t I, typename... Ts> auto field(Ts... values) { return values...[I]; } template <typename... Ts> auto last(Ts... values) { return values...[sizeof...(Ts) - 1]; } ``` With Clang 21 in `-std=c++2c` mode, `Record<int, double, std::string>::field_type<2>` is `std::string`, `field<1>(7, 0.5, "eu-1")` returns `0.5`, and `last(7, 0.5, "eu-1")` returns the string literal. ## The rules - The name before `...[` must be an **identifier that denotes a pack** — a template parameter pack or a function parameter pack. It cannot index a `std::tuple` or an array. - The index must be a **converted constant expression of type `std::size_t`**, so it is known at compile time; a run-time index needs another tool, such as a switch or a table of function pointers. - The index must satisfy **`0 <= I < sizeof...(pack)`**. Clang 21 diagnoses violations at instantiation: `invalid index 3 for pack 'v' of size 3`, and `invalid index 0 for pack 'v' of size 0` for an empty pack. - A pack index is itself a pack expansion in the standard's terms, but it produces one element, so no trailing `...` is written. ## A change that can break old code Before C++26, `T...[1]` inside a function parameter list declared a **pack of unnamed array parameters**: `template <typename... T> void f(T... [1]);` meant parameters of types `T[1]...`, adjusted to pointers. The C++26 compatibility annex records that this now parses as a pack-index-specifier, making such declarations ill-formed. Naming the parameter, as in `T... ptr[1]`, keeps the old meaning. The pattern is rare, but it is the one way pack indexing can break existing code. ## Comparison | Need | Before C++26 | C++26 | |---|---|---| | Type of element `I` | `std::tuple_element_t<I, std::tuple<Ts...>>` or a recursive helper | `Ts...[I]` | | Value of element `I` | `std::get<I>(std::forward_as_tuple(values...))` | `values...[I]` | | First type | `std::tuple_element_t<0, std::tuple<Ts...>>` | `Ts...[0]` | | Last value | recursion or tuple with `sizeof...(Ts) - 1` | `values...[sizeof...(Ts) - 1]` | ## When to keep the older forms Pack indexing does not make every older technique obsolete: - **Portability**: a library that must build with C++17 or C++20 compilers keeps a helper alias and switches its implementation by feature test. - **Run-time positions**: indexing needs a constant, so dispatch on a run-time index still needs a switch, a fold or a table. - **Real tuples**: when the values must be stored, not just reached, a `std::tuple` or a record type is still the container, and `std::get` remains its accessor. - **Iteration over every element**: folds and expansions remain the tool; looping over indices with pack indexing would need an index sequence anyway. ## Toolchain status Pack indexing is part of the C++26 draft, and compiler support is partial across toolchains. Clang 21 accepts it in `-std=c++2c` mode, and in `-std=c++23` mode warns `pack indexing is a C++2c extension`. A codebase building as C++17 or C++20 keeps the tuple or recursive forms behind a small alias, such as `type_at_t<I, Ts...>`, that can switch to pack indexing once every supported compiler has it. ## Why interviewers ask It is a newer feature, so it comes up as a differentiator: knowing the syntax, the constant-index rule and what it replaces shows a candidate following the standard's evolution rather than only writing C++17 idioms.
- Can values...[i] take an index that is only known at run time?No. The index must be a converted constant expression of type `std::size_t`, because each element may have a different type and the expression's type must be fixed at compile time. For a run-time index you dispatch over the possible positions yourself, for example with a fold that compares `i` against each compile-time index and acts on the match.
- How would you get the first type of a pack in C++20 without pack indexing?Use `std::tuple_element_t<0, std::tuple<Ts...>>`, or a small helper: a primary template `first<Ts...>` with a partial specialization `first<T, Rest...>` exposing `using type = T;`. Both instantiate extra templates per pack. Wrapping either in an alias lets the code switch to `Ts...[0]` once the toolchain supports C++26.
- values...[i] accepts any integer variable as the index.
- Pack indexing works on std::tuple and arrays as well as on packs.
- An out-of-range pack index is undefined behaviour at run time.
- Pack indexing is part of C++23.
- Every compiler supporting C++20 already accepts Ts...[I].
Pack indexing is C++26 (draft N5050); Apple clang 21 accepts it with -std=c++2c and warns that it is an extension in C++23 mode.