Skip to content
Tutorials Center
My learning

Topics

Lesson 90 of 171 ยท Templates and Generic Programming

Variadic Templates and Fold Expressions

Learn C++ variadic templates - parameter packs, pack expansion, fold expressions, a type-safe print and sum, forwarding references and perfect forwarding with std::forward.

std::println("{} + {} = {}", a, b, a + b) takes three arguments after the format string. Another call might take zero or ten, each of a different type, and every one is checked at compile time. std::make_unique<Widget>(1, "two", 3.0) passes any number of arguments straight to a constructor. std::tuple<int, std::string, double> holds any number of types. All of these are variadic templates: templates that accept a variable number of template arguments.

C's old answer to "any number of arguments" was printf-style ..., which throws away the types and turns a mismatched %d into undefined behavior. Variadic templates keep every argument's real type. In this lesson you'll learn parameter packs and how to expand them, the C++17 fold expressions that make most variadic code a one-liner, and perfect forwarding with forwarding references and std::forward, the technique behind emplace_back and make_unique.

Parameter packs

A template parameter pack is written typename... Ts; it stands for zero or more types. A function parameter pack Ts... args declares a matching list of function parameters. sizeof...(args) tells you how many there are.

C++ Counting the arguments in a pack

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.

The ... appears in three different roles; reading them right is half the battle:

  • typename... Ts declares a pack of types (the dots come before the name).
  • const Ts&... args declares a pack of function parameters, one per type: const int&, const double&, and so on.
  • args... or f(args)... expands a pack (the dots come after a pattern). The pattern is repeated for every element, separated by commas.

Pack expansion works on a whole pattern, not just a name. If args holds a, b, c:

You write It expands to
g(args...) g(a, b, c)
g(h(args)...) g(h(a), h(b), h(c))
g(h(args...)) g(h(a, b, c))
g(args * 2 ...) g(a * 2, b * 2, c * 2)
std::tuple<Ts...> std::tuple<int, double, char>
std::vector<Ts>... (in a list) std::vector<int>, std::vector<double>, std::vector<char>

Expansions are only allowed in places where a comma-separated list makes sense: function call arguments, template argument lists, initializer lists, base class lists and a few others. You can't write args...; as a statement on its own.

Recursion: the pre-C++17 style

Before fold expressions, the standard way to process a pack was recursion: handle the first argument, then call yourself with the rest. You'll still see this in older code and it's still the right tool when each step needs real logic.

C++ Processing a pack recursively

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.

Each call instantiates a new function with one fewer parameter: print_all<int, double, char, std::string, bool>, then print_all<double, char, std::string, bool>, and so on, down to the plain print_all(). The optimizer usually flattens the whole chain. The if constexpr decides at compile time whether a comma is needed; you'll learn it properly in Compile-Time Programming.

Fold expressions

C++17 added fold expressions, which apply a binary operator across a whole pack in one go. There are four forms; op is any binary operator (+, *, &&, ||, ,, <<, ...) and init is a starting value:

Form Name Expands to (for packs a, b, c)
(args op ...) unary right fold a op (b op c)
(... op args) unary left fold (a op b) op c
(args op ... op init) binary right fold a op (b op (c op init))
(init op ... op args) binary left fold ((init op a) op b) op c

The parentheses are part of the syntax and can't be omitted.

C++ Folds for sum, all_of and printing

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.

Some details that matter in practice:

  • Empty packs. A unary fold over an empty pack is only allowed for && (gives true), || (gives false) and , (gives void()). For everything else, use a binary fold with an identity value, as sum does with + 0.
  • Order of evaluation. The comma fold (f(args), ...) calls f strictly left to right, which makes it the go-to pattern for "do something with each argument". The && and || folds short-circuit, just like the normal operators.
  • Result type. sum(1, 2.5, 3.25f) returns double because the fold is just ordinary arithmetic with the usual promotions.

Folding with a lambda

For more complex per-element work, wrap it in a lambda and fold over the comma operator. This also gives you the index of each element:

C++ Per-element work with a lambda and a comma fold

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.

max_of takes the first argument separately so there's always a starting value (calling it with no arguments is a compile error, which is what you want). std::common_type_t<T, Ts...> from <type_traits> finds a type every argument converts to; here int and double give double. That's something std::max({...}) can't do, since its initializer list needs all elements to have one type already. For two arguments you'd still just use std::max; see Type Traits and Metaprogramming for more on common_type.

Variadic class templates

Classes can take packs too. std::tuple<Ts...> and std::variant<Ts...> are the famous ones. A simple and useful pattern is inheriting from every type in a pack, which is how the "overloaded lambdas" helper for std::visit works:

C++
template <typename... Fs>
struct overloaded : Fs... {        // inherit from every lambda type
    using Fs::operator()...;       // bring every operator() into scope
};
C++
std::variant<int, std::string> v = "text";
std::visit(overloaded{
    [](int i) { std::println("int {}", i); },
    [](const std::string& s) { std::println("string {}", s); },
}, v);

Thanks to CTAD for aggregates (C++20), overloaded{...} deduces Fs from the lambdas you pass, with no deduction guide needed.

Forwarding references and std::forward

Now the most important practical use of variadic templates: writing a function that passes its arguments on to another function exactly as it received them. std::make_unique<T>(args...) must pass lvalues as lvalues (so they're copied) and rvalues as rvalues (so they're moved). Doing that correctly is called perfect forwarding.

It relies on a special rule. In a function template, a parameter declared T&& where T is deduced right there is not an rvalue reference: it's a forwarding reference (sometimes called a universal reference) that binds to anything.

  • Pass an lvalue std::string โ†’ T is deduced as std::string&, and T&& collapses to std::string&.
  • Pass an rvalue std::string โ†’ T is deduced as std::string, and the parameter is std::string&&.

The collapsing rule is simple: any & wins. & &, & && and && & all become &; only && && stays &&.

But inside the function, the parameter has a name, so it's an lvalue (recall Move Semantics). std::forward<T>(arg) restores the original category: it casts to an rvalue only if T says the caller passed an rvalue.

C++ Perfect forwarding with std::forward

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.

Read the output for make<Pair>: the lvalue t is copied exactly once (into the constructor's by-value parameter) and the temporary is only ever moved; the other two moves are Pair's constructor moving its parameters into its members. Without std::forward, both arguments would have been copied. (GCC happens to build the second argument first; the order in which function arguments are evaluated is unspecified.) The expression std::forward<Args>(args)... is a pack expansion of the pattern std::forward<Args>(args), forwarding each argument with its own type.

Forwarding pitfalls

  • Only a deduced T&& is a forwarding reference. void f(std::string&& s) is a plain rvalue reference. So is void Stack<T>::push(T&& x) inside a class template, because T was fixed when the class was instantiated. auto&& is a forwarding reference (used in range-for loops and generic lambdas).
  • Forward each argument at most once. After std::forward<T>(x) an rvalue may have been moved from. Don't forward the same argument into two calls.
  • Use std::move for rvalue references and std::forward for forwarding references. Mixing them up either copies needlessly or moves things the caller still owns.
  • Forwarding references are greedy. A constructor template template <typename T> Widget(T&&) can beat the copy constructor for a non-const Widget lvalue, because Widget& is a better match than const Widget&. Constrain it with a concept, for example requires (!std::same_as<std::remove_cvref_t<T>, Widget>).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. If args holds a, b, c, what does f(g(args)...) expand to?

    The dots expand the whole pattern g(args) once per element, separated by commas, inside f's argument list.
  2. 2. template <typename... Ts> auto total(Ts... xs) { return (xs + ...); } What happens with total()?

    Only &&, || and the comma operator have a defined value for empty unary folds. Use a binary fold such as (xs + ... + 0) to support zero arguments.
  3. 3. Which parameter is a forwarding reference?

    Only T&& where T is deduced in that very function template (or auto&&) is a forwarding reference. const T&& and T&& with T fixed by the class are rvalue references.
  4. 4. template <typename T> void w(T&& x) { sink(x); } is called as w(std::string{"hi"}). Which sink overload runs?

    A named reference is an lvalue regardless of its type. To pass the rvalue on as an rvalue, write sink(std::forward<T>(x)).
  5. 5. In what order does (log(args), ...) call log for args a, b, c?

    A comma fold uses the built-in comma operator, which evaluates its left operand completely before the right, so the calls happen left to right.

Summary

  • typename... Ts declares a template parameter pack, Ts... args a function parameter pack, and sizeof...(args) counts them.
  • pattern... expands a pack by repeating the pattern for each element, in argument lists, template argument lists and initializers.
  • Recursion (first + rest) was the pre-C++17 way to process packs; fold expressions like (args + ... + 0), (... && args) and (f(args), ...) usually replace it.
  • Unary folds over empty packs work only for &&, || and ,; give other operators an initial value.
  • A deduced T&& is a forwarding reference; reference collapsing makes it an lvalue or rvalue reference depending on the argument.
  • std::forward<T>(arg) passes an argument on with its original value category; std::forward<Args>(args)... is the core of make_unique and emplace_back.

Next: Compile-Time Programming: constexpr and consteval, where you'll run code while your program compiles.