C++ Reference ยท Types and Language Concepts
Variadic templates and parameter packs
Reference for C++ variadic templates: parameter packs, pack expansion, sizeof..., fold expressions, perfect forwarding and recursive unpacking.
A variadic template accepts any number of template arguments. The arguments are collected in a parameter pack, declared with an ellipsis: typename... Ts is a pack of types, and Ts... args a pack of function parameters. You cannot use a pack as a value; you expand it with ..., which repeats the pattern next to it once for each element.
Variadic templates replaced C's va_list with a type-safe mechanism. They power std::tuple, std::make_unique, std::print, emplace_back and many other functions that must pass any number of arguments of any types.
template <typename... Ts> void show(const Ts&... args); // Ts is a type pack, args is a function parameter pack
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template <typename... Ts> void f(Ts... args); // declare packs template <typename... Ts> void g(Ts&&... args) { // use: expand with ... h(std::forward<Ts>(args)...); // pattern std::forward<Ts>(args) repeated } sizeof...(Ts) // number of elements in the pack
| Fold form (C++17) | Expands to |
|---|---|
(pack op ...) unary right fold |
p1 op (p2 op (... op pN)) |
(... op pack) unary left fold |
((p1 op p2) op ...) op pN |
(init op ... op pack) binary left fold |
((init op p1) op p2) ... op pN |
(pack op ... op init) binary right fold |
p1 op (... op (pN op init)) |
An empty unary fold is allowed only for && (true), || (false) and , (void()).
Usage notes
- Expansion repeats the pattern.
f(g(args)...)callsgon each element and passes all results tof;f(g(args...))passes all elements to a singleg. The position of the...decides. - Pack elements cannot be indexed with
args[i]. Use fold expressions, recursion,std::get<I>(std::tuple{args...}), or pack indexing (args...[0], C++26; GCC 15 accepts it even in C++23 mode, but treat it as new). - Base case. Recursive unpacking needs a non-variadic overload or
if constexpr (sizeof...(rest) > 0)to stop. - Forward with
std::forward<Ts>(args)...to keep lvalue/rvalue-ness;Ts&&...with deducedTsis a pack of forwarding references. - Packs can appear in many places: base class lists (
struct X : Ts... {}), initializer lists (int a[] = {args...};), lambda captures ([...xs = std::move(xs)]in C++20), template argument lists,usingdeclarations (C++17) andrequiresclauses. - Fold with a comma is the usual way to run an action once per element. Order is left to right.
- Empty packs are fine for function calls, but a fold over
+with an empty pack is an error; give an initial value:(0 + ... + xs). - Type-safe alternative to
printf-style functions: C varargs (...without a template) lose type information, and are reserved for C interop. - Compile-time cost: each distinct argument list is a separate instantiation, and recursive unpacking creates one function per element.
- Constraining the pack:
template <std::integral... Ts>requires every element to satisfy the concept.