CKTIS / C++
Parameter pack and pack expansion
A parameter pack accepts zero or more arguments. A pack expansion applies a pattern to successive elements using `...`. Distinguish declaring a pack, expanding it, and counting its elements with `sizeof...`.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
template<class... Ts>
void relay(Ts... args) {
target(args...);
}
// Pack size: sizeof...(Ts) or sizeof...(args)parameter pack / pack expansion names two related concepts. In template<class... Ts>, Ts names a template parameter pack of types. In Ts... args, args names a function parameter pack; this declaration also expands Ts.
A pack expansion is a pattern with an ellipsis: args... produces a list of elements, not one object. Packs expanded together must have equal lengths. Writing target(args) does not expand the pack; use target(args...). sizeof...(args) gives the element count, including zero.
In the example, {0, args...} produces array initializers. The leading 0 ensures that the array has an element even for an empty pack. The calls use only int values; expansion does not remove the array element initialization requirements.
Do not confuse Ts... args with the ellipsis in void f(int, ...): that is a different argument mechanism. This entry targets C++23; the linked, evolving draft also contains newer constructs. Do not treat the entire page as describing only C++23.
Examples
#include <iostream>
template<class... Ts>
void report(Ts... args) {
std::cout << sizeof...(Ts) << ':';
const int values[] = {0, args...};
for (int value : values) {
std::cout << ' ' << value;
}
std::cout << '\n';
}
int main() {
report();
report(4, 7, 9);
}
Result
0: 0 3: 0 4 7 9
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.