[c++] Variadic template improvements

C++11 introduced the concept of variadic templates, allowing functions and classes to accept an arbitrary number of arguments of different types. Variadic templates are defined using template parameter packs, denoted by an ellipsis (...), which represent a sequence of template parameters.

In C++17, variadic templates received several improvements, making them more flexible and convenient to use. Let’s explore some of these enhancements.

Fold Expressions

One of the significant improvements in C++17 is the introduction of fold expressions. Fold expressions allow performing operations on all elements of a parameter pack, such as expanding a pack and applying a binary operator between its elements.

For example, consider the following code snippet:

template <typename... Args>
auto sum(Args... args) {
    return (args + ...);
}

int result = sum(1, 2, 3, 4);

In this example, the sum function uses a fold expression (args + ...) to sum all the arguments passed to it. The ... is used to expand the pack, and the + operator is applied between the elements.

Class Template Argument Deduction

C++17 also introduced class template argument deduction for class templates with a deduction guide. This allows class templates to deduce template arguments from their constructors, enabling more concise and intuitive syntax when working with variadic templates.

For example:

template <typename... Args>
struct Tuple {};

Tuple t{1, 2, 3};  // Deduction guides help the compiler deduce the template arguments

In this example, the deduction guides for the Tuple class template assist the compiler in deducing the template arguments from the constructor, allowing us to create an instance of Tuple without specifying the template arguments explicitly.

References: