In C++, what do typename... Ts and Ts... args declare in a variadic log_event template, and what does sizeof...(Ts) return?
answer
- zero or more of something
- one pack of types, one of values
- the ellipsis after a pattern expands it
- a count, as std::size_t
basics
~20 stypename... Ts declares a template parameter pack of zero or more types; Ts... args declares a function parameter pack with one parameter per type. sizeof...(Ts) is the number of elements in the pack, a compile-time std::size_t, not a size in bytes.
solid answer
~40 sIn `template <typename... Ts> void log_event(const char* name, Ts... args)`, `typename... Ts` is a **template parameter pack**: it stands for zero or more types. `Ts... args` is a **function parameter pack**: one function parameter per type in `Ts`. A call like `log_event("flush", "disk0", 2.5)` deduces `Ts = {const char*, double}`; `log_event("boot")` deduces an empty pack. You cannot use `args` directly as one value; you **expand** it by writing a pattern followed by `...`, for example `to_text(args)...`, which becomes a comma-separated list `to_text(a0), to_text(a1)`. `sizeof...(Ts)` — or `sizeof...(args)` — yields the number of elements as a `std::size_t` constant expression, not a byte size. Forgetting the expansion is a compile error: clang reports `expression contains unexpanded parameter pack 'args'`.
code
cpp · 21 lines#include <cstdio>
#include <string>
#include <vector>
std::string to_text(int v) { return std::to_string(v); }
std::string to_text(double v) { return std::to_string(v); }
std::string to_text(const char* s) { return s; }
template <typename... Ts> // template parameter pack: zero or more types
void log_event(const char* name, Ts... args) { // function parameter pack: one parameter per type
std::vector<std::string> fields{to_text(args)...}; // expands to to_text(a0), to_text(a1), ...
std::printf("%s: %zu fields", name, sizeof...(Ts)); // element count, not bytes
for (const auto& f : fields) std::printf(" [%s]", f.c_str());
std::printf("\n");
}
int main() {
log_event("boot"); // Ts is empty
log_event("cpu", 93); // Ts = {int}
log_event("flush", "disk0", 2.5); // Ts = {const char*, double}
}go deeper
Recall the two packs, typename... Ts for types and Ts... args for function parameters, that a pack can be empty, and that sizeof... counts elements.
Explain expansion as a pattern followed by an ellipsis, the unexpanded-pack error, and the position rules that keep packs last in class templates.
Use packs to build type-safe interfaces and know which contexts allow expansion and how non-trailing function parameter packs block deduction.
Weigh variadic templates against fixed overloads or initializer lists for an API, considering compile time, error messages and the code size of many instantiations.
## The two kinds of pack A **parameter pack** is a template or function parameter that accepts zero or more arguments. C++ has two kinds, and a variadic function usually uses both: ```cpp template <typename... Ts> // template parameter pack void log_event(const char* name, Ts... args); // function parameter pack ``` - `typename... Ts` (or `class... Ts`) declares a **template parameter pack** of types. Packs of values exist too: `template <int... Ns>`. - `Ts... args` declares a **function parameter pack**. Its type is a pattern, `Ts`, expanded so that there is one parameter per type in the pack. The position of the ellipsis carries the meaning: before the name it **declares** a pack; after a pattern it **expands** one. ## How the pack gets its contents For function templates, the pack is normally deduced from the call: | Call | `Ts` deduced as | `sizeof...(Ts)` | |---|---|---| | `log_event("boot")` | empty | 0 | | `log_event("cpu", 93)` | `int` | 1 | | `log_event("flush", "disk0", 2.5)` | `const char*`, `double` | 2 | The pack may be empty, and code using it has to work then too. A pack can also be given explicitly, as in `log_event<int, long>("x", 1, 2)`. Position rules: - In a **primary class template**, a template parameter pack must be the **last** template parameter: `template <typename... Ts, typename U> struct Record;` is ill-formed. - In a **function template**, a pack may be followed by another template parameter only if that parameter can be deduced from the function parameters or has a default. - A function parameter pack that is **not last** in the parameter list is a non-deduced context, so `template <class... Ts> void f(Ts..., int)` deduces nothing for `Ts` from a call. ## Using a pack: expansion A pack is not a value or a type you can use on its own. Every mention must be inside a **pack expansion**: a pattern followed by `...`. The compiler repeats the pattern once per element and separates the copies with commas: ```cpp std::vector<std::string> fields{to_text(args)...}; // for Ts = {const char*, double}: { to_text(args#0), to_text(args#1) } ``` Writing `g(args)` without the ellipsis is an error. Clang 21 says: ```plaintext error: expression contains unexpanded parameter pack 'args' ``` Expansion is allowed only in certain places — function call arguments, braced initializers, template argument lists, base class lists, member initializer lists and a few more — because each needs a comma-separated list in the grammar. ## Packs of values Packs are not limited to types. `template <int... Ns>` declares a pack of `int` values, and `template <std::size_t... Is>` is how code receives a compile-time list of indices. The standard library generates such lists with `std::make_index_sequence<N>`, which produces `std::index_sequence<0, 1, ..., N-1>`; a function template taking `std::index_sequence<Is...>` deduces `Is` as that list and can expand patterns like `values[Is]...`. The rules are the same as for type packs: declare with the ellipsis before the name, expand with the ellipsis after a pattern, and count with `sizeof...`. ## sizeof... `sizeof...(Ts)` and `sizeof...(args)` give the **number of elements** in the pack as a prvalue of type `std::size_t`. It is a constant expression, so it can size an array, select a branch with `if constexpr`, or appear in a `static_assert`. It is unrelated to `sizeof`, which measures bytes: `sizeof...(Ts)` for `{char, double}` is 2, whatever the types' sizes. ## A typical first variadic function The telemetry helper converts each argument to text and prints the count and the fields: - `to_text` is overloaded for `int`, `double` and `const char*`, so each element picks its own overload during expansion. - The vector uses braces, so an empty pack produces an empty vector rather than a syntax problem. - Every argument type is checked at compile time: passing a type with no `to_text` overload fails to compile, unlike a `printf` format mismatch, which is undefined behaviour at run time. That last point is why variadic templates replaced C-style `...` varargs in modern interfaces: the types travel with the arguments. ## Why interviewers ask Junior questions check the vocabulary — the two kinds of pack, expansion with `...`, and `sizeof...` as a count. Follow-ups usually move to fold expressions and to how the expansion pattern decides what is repeated.
- Can a class template have a parameter pack followed by another template parameter, as in template<typename... Ts, typename U> struct Record?Not in a primary class template: there a template parameter pack must be the last template parameter. Function templates are looser — a pack may be followed by a parameter that is deduced from the function arguments or has a default. Partial specializations can place packs inside patterns, such as `Record<std::pair<Ts, int>...>`.
- Why is a variadic template safer than a C-style ... function such as printf?A C-style variadic function receives untyped arguments after default promotions, and a mismatch between the format string and an argument is undefined behaviour at run time. A function parameter pack keeps every argument's type, so each one is checked and dispatched at compile time, and an unsupported type is a compile error.
- sizeof...(Ts) returns the total size in bytes of all the types.
- A parameter pack must contain at least one element.
- You can pass args to another function without writing the ellipsis.
- A function parameter pack in the middle of the parameter list is still deduced from the call.
- Variadic templates and C-style varargs both lose the argument types.