Let’s face it: newer features introduced in and after C++11 are still a mystery for most novices and beginners. Yet, some of them are powerful tools that you could take advantage of in your next project. As I’m currently researching and relearning a lot of the modern C++ features, I figured why not write some blog posts about them. Free according to the motto:

Teaching is the highest form of understanding

Aristotle

In this article, i am going to teach you a tiny bit about such a feature, namely variadic templates. I explain what they are and how they can be applied. I assume that you have some knowledge about C++ templates.

What are Variadic Templates?

In short: variadic templates come in handy, when you need a function that takes an unspecified number of arguments implemented for a diverse set of types.

Ok, this explanation is pretty technical. Let us look at a toy example:

The function make_list accepts any number of arguments (of the same type) and creates a standard vector (or list) of the provided elements. Why do we need two functions for it? Well, the first function is always applied if we provide exactly one argument. The second function is applied if we provide more than one argument.

Note that the second function recursively calls itself. With variadic templates, we establish some kind of recursion at compile-time. The first function is basically the termination condition for this recursion.

Let’s see how we would call the function:

Type-safety and Performance

Variadic templates enable us to write type-safe functions. Unlike variadic functions (such as printf), variadic templates are evaluated at compile-time. For instance, it is not possible to do the following without running into a compile-time error, as multiple types are used instead of one:

This prevents us from running into undefined behaviors or run-time errors.

If you are curios about the performance, variadic templates are possibly the least to worry about. In fact, as they are evaulated at compile-time and seldomly called with more than five or six arguments, the resulting code is comparably small. Moreover, modern compilers are aggressively optimizing code (e.g., inlining, unfolding, constant propagation, etc.).

Discussion

As typical for templates, using them is significantly easier than implementing them. This trade-off is very typical for C++, which is why many people consider C++ to be too difficult or even “unbearable” nowadays. Mainstream languages, such as Java or Python are indeed less complex in their handling. However, on the plus side, C++ is more powerful and provides more fine-grained control for developers. Experienced developers are therefore less dependend on the infrastructure and are able to increase the performance of their applications to a great extent.

In my opinion, modern C++ has its place and provides a lot of neat features. The art is in creating user-friendly but also efficient frameworks and interfaces for other people by putting more effort into the implementation.

Links to Resources

I want to conclude this article by providing some helpful resources I looked at while preparing this brief article: