Variadic Templates and Parameter Packs
Use parameter packs and fold expressions to write variadic templates.
Variadic Templates and Parameter Packs is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Variable Number of Template Arguments
Variadic templates accept any number of template arguments. Used to write functions like std::make_unique, std::tuple, and printf-style logging.
Parameter Pack Syntax
Use typename... Ts to declare a type parameter pack, and Ts... to expand it.
template <typename... Args>
void print_all(Args... args) {
// ...
}Pack Expansion
The ... after an expression expands the pack into a comma-separated list.
template <typename... Args>
void pass_through(Args... args) {
other_func(args...); // pass each arg
}Recursive Variadic Templates (Old Style)
Pre-C++17, variadic functions were typically written recursively — process one arg, then recurse with the rest.
// base case
void log() {}
// recursive case
template <typename T, typename... Rest>
void log(T first, Rest... rest) {
std::cout << first;
log(rest...);
}Fold Expressions (C++17)
C++17 added fold expressions — much cleaner than recursion. Apply an operator across a pack.
template <typename... Args>
auto sum(Args... args) {
return (args + ...); // fold +
}
std::cout << sum(1, 2, 3, 4); // 10Fold Operators
Four fold forms:
(... op pack)— left fold over a unary expansion(pack op ...)— right fold(init op ... op pack)— left fold with init(pack op ... op init)— right fold with init
Printing All Arguments
A common idiom with comma fold.
template <typename... Args>
void print_all(Args... args) {
((std::cout << args << " "), ...); // comma fold
}
print_all(1, "hi", 3.14); // 1 hi 3.14Counting Arguments
sizeof...(pack) returns the number of items in a pack at compile time.
template <typename... Args>
void info(Args... args) {
std::cout << "count = " << sizeof...(args);
}Variadic Class Templates
Class templates can also have parameter packs. Example: std::tuple<Ts...>.
template <typename... Ts>
class Tuple {
// implementation uses recursion or storage tricks
};Perfect Forwarding with Packs
Combine variadic templates with std::forward to perfectly forward arbitrary arguments to a constructor.
template <typename T, typename... Args>
std::unique_ptr<T> my_make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}Type Pack Manipulation
With template specialization or fold expressions, you can implement compile-time list operations: first, last, head/tail decomposition.
Pitfalls
Common issues:
- Forgetting to expand the pack (
argsvsargs...) - Order of evaluation in fold expressions (left vs right matters for non-associative ops)
- Increased compile time with deep recursion
Quick Check
Which C++17 feature simplifies summing all elements of a parameter pack?
Recap
Variadic templates handle a variable number of template arguments via parameter packs. C++17 fold expressions replace recursive variadic code with concise expressions. Combined with perfect forwarding, they power factory functions and generic dispatch.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Variadic Templates and Parameter Packs” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Variadic Templates and Parameter Packs”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Variadic Templates and Parameter Packs”?
Use parameter packs and fold expressions to write variadic templates. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Variadic Templates and Parameter Packs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Function and Class Templates Revisited
- Template Specialization Partial and Full
- Variadic Templates and Parameter Packs
- constexpr Functions and if constexpr