C++ Variadic Templates — No More va_list Type Blunders
Replace va_list runtime dangers with compile-time safe variadic templates.
Imagine you're a chef who can cook any number of dishes in one go — whether someone orders 1 plate or 15, you have a single recipe that adapts. Variadic templates are exactly that for C++ functions and classes: one template definition that handles any number of arguments of any types, all resolved at compile time with zero runtime overhead. Before they existed, you'd have to write a separate recipe for every possible order size. Now you write it once and the compiler does the rest.
Every production C++ codebase eventually hits a wall where you need a function that accepts a truly flexible number of arguments — a type-safe printf replacement, a tuple, an event emitter, a dependency injection container. The C-style variadic approach (va_list, va_arg) was always there, but it's a loaded gun with the safety off: no type checking, no compile-time introspection, and a debugging experience that makes grown engineers cry. Variadic templates, introduced in C++11 and dramatically improved by C++17 fold expressions, solve this at the language level rather than the runtime level.
The core problem variadic templates solve is the N-arity explosion. Without them, libraries like std::tuple, std::make_shared, std::bind, and std::variant would require code generators or preprocessor macro soup to support arbitrary argument counts. With variadic templates, the compiler itself becomes the code generator — it stamps out exactly the specialisations needed for your call sites and nothing more. That's both a correctness win and a performance win.
By the end of this article you'll be able to write recursive and fold-expression-based variadic templates from scratch, understand how pack expansion interacts with different contexts, spot the subtle ordering and deduction edge cases that trip up even experienced engineers, and make confident decisions about when variadic templates are the right tool versus a simpler alternative like initializer_list or a variadic lambda.
Parameter Packs — What the Compiler Actually Sees
A parameter pack is not a container. That's the most important thing to internalise before anything else. It's a compile-time placeholder that represents zero or more template arguments. The syntax typename... Types declares a template parameter pack named Types. When you later write Types... in an expression, you're expanding that pack — asking the compiler to repeat the surrounding pattern once for each type in the pack, separated by commas.
There are two flavours: template parameter packs (typename... Types) and function parameter packs (Types... args). The function parameter pack binds the actual runtime values to the compile-time types. When you write sizeof...(args) you get the count at compile time — this is resolved entirely before the program runs.
Expansion context matters enormously. You can expand a pack in a function argument list, a base class list, an initialiser list, a template argument list, and (since C++17) a fold expression. You cannot expand it in arbitrary positions — trying to use a pack without expanding it is a compile error. The pattern being expanded can be as complex as a full type transformation: std::forward<Types>(args)... expands to std::forward<T1>(a1), std::forward<T2>(a2), ... — the entire pattern repeats, not just the pack name.
f(args...) — gives no guaranteed evaluation order in C++14 and earlier. If you need strict left-to-right order (e.g., for printing or sequenced side effects), expand inside a braced initialiser list or use a fold expression with operator, in C++17. Relying on argument evaluation order is one of the stealthiest UB sources in variadic template code.Recursive Unpacking vs. C++17 Fold Expressions — Choosing the Right Tool
Before C++17, the only way to process each element in a pack was recursive template instantiation: peel the first argument off, handle it, then recurse on the rest. This works, but it creates O(N) distinct template instantiations, each with its own function body. For N=10 that's fine; for N=100 it starts to bloat compile times and binary size meaningfully.
C++17 fold expressions collapse the entire expansion into a single expression the compiler can evaluate in one pass. The syntax (args + ...) is a unary right fold — it expands to arg0 + (arg1 + (arg2 + ...)). A unary left fold (... + args) expands from the other direction. Binary folds let you supply an identity value: (0 + ... + args) ensures the expression is valid even for an empty pack.
The practical rule: prefer fold expressions for reduction operations (sum, product, logical AND/OR, string concatenation) and for simple per-element side effects using operator,. Use recursion when you need to carry state between iterations, build a data structure element-by-element with non-trivial logic, or target C++11/14 codebases. The two techniques compose well — a fold expression can call a helper that is itself a recursive template.
Production Patterns — Type-Safe Logger, tuple_for_each, and Perfect Forwarding
The real test of understanding variadic templates is applying them to problems you'd actually ship. Three patterns appear constantly in production C++ codebases: a type-safe variadic logger that avoids printf's undefined behaviour, a compile-time iteration over tuple elements, and perfect forwarding through a variadic wrapper.
Perfect forwarding with variadic templates is the backbone of factory functions, emplace methods, and middleware chains. The idiom std::forward<Types>(args)... preserves the value category (lvalue vs rvalue) of every argument independently — something impossible with C-style variadics or with a fixed-arity overload set.
Iterating over a tuple's elements requires index_sequence machinery because std::get<N> needs a compile-time constant. The helper std::index_sequence_for<Types...> generates a pack of compile-time indices matching the pack size, letting you expand a get and a callable in one shot. This pattern is the foundation of serialisation libraries, reflection utilities, and test frameworks that introspect argument types at compile time.
These three patterns together cover the vast majority of variadic template use cases you'll encounter outside of metaprogramming libraries. Master them and you'll be able to read — and contribute to — standard library implementations and frameworks like fmtlib, Boost.Hana, and range-v3.
std::forward<Types>(args) in the expansion std::forward<Types>(args)... operates independently — T0 might be an lvalue ref while T1 is an rvalue ref, and forward handles them separately. If you wrote std::move(args)... instead, you'd unconditionally move every argument, breaking callers who pass named lvalues they still need after the call. This is a question interviewers use specifically to test whether a candidate truly understands perfect forwarding versus cargo-culting it.Internals, Compile-Time Cost, and When NOT to Use Variadic Templates
Every distinct call site with a unique combination of argument types generates a separate template instantiation. Call sumAll(1, 2, 3) and sumAll(1.0, 2.0, 3.0) and the compiler stamps out two different functions. This is the source of variadic templates' zero runtime overhead — but it's also the source of compile-time and binary-size overhead that can genuinely hurt large projects.
Recursive variadic templates are especially expensive to compile. A depth-N recursive instantiation chain creates N+1 distinct types and N+1 function bodies. For deeply recursive packs (think: 20-30 elements in a mixin hierarchy or a tuple), this compounds. C++17 fold expressions mitigate this by collapsing the expansion into a single expression, dramatically reducing the number of instantiations the compiler must produce.
The right tool question is often overlooked. Use std::initializer_list<T> when all arguments are the same type — it compiles faster, binary is smaller, and the intent is clearer. Use a std::vector parameter when the count is runtime-dynamic. Reach for variadic templates specifically when you need: heterogeneous types, perfect forwarding, compile-time type introspection, or pack expansion into a base-class list. Using variadic templates where initializer_list would do is over-engineering — a real code smell in production reviews.
Key takeaways
sizeof...(args) costs nothing at runtime because it's resolved entirely during compilation.std::forward<Types>(args)... forwards each argument independently, preserving its individual value categorystd::initializer_list<T> when all arguments share one typeInterview Questions on This Topic
Frequently Asked Questions
That's C++ Advanced. Mark it forged?
4 min read · try the examples if you haven't