0Pricing
C++ Academy · Lesson

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 — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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);   // 10

Fold 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.14

Counting 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 (args vs args...)
  • 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.

Frequently asked questions

Is the “Variadic Templates and Parameter Packs” lesson free?

Yes — the full text of “Variadic Templates and Parameter Packs” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Function and Class Templates Revisited
  2. Template Specialization Partial and Full
  3. Variadic Templates and Parameter Packs
  4. constexpr Functions and if constexpr
← Back to C++ Academy