Variadic Templates in C++
Variadic templates accept any number of arguments: template <typename... Args> captures the pack, sizeof...(Args) counts it, and recursion (or C++17 fold expressions) processes it. One definition covers print(a), print(a,b,c), and beyond — the machinery behind tuple, std::format-style formatting, and perfect-forwarding factories like make_unique.
The classic shape peels one parameter (head) and recurses on the rest, with a zero-argument base case stopping the chain. Fold expressions ((std::cout << ... << args)) collapse packs in a single expression — the modern, recursion-free way.
Packs capture everything
typename... Args bundles all remaining arguments; Args... expands the pack wherever the elements belong.
Recurse head-plus-rest
Process the first argument, recurse on the remainder, and terminate with a no-argument overload as the base case.
Fold expressions (C++17)
(cout << ... << args) expands to chained insertions in one line — no recursion, no base case, no helper.
Forward perfectly
Args&&... with std::forward preserves value categories into nested calls — how emplace and make_unique forward constructor args.
#include <iostream> template <typename... Args>void printAll(Args... args) { ((std::cout << args << " "), ...); std::cout << std::endl;} int main() { printAll(1, 2.5, "three"); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.