Skip to content

C++

Variadic functions – Part 2: C++11 variadic templates

In the previous article, we have seen an old C-style way of defining the variadic functions – via va_args macros. We have also learned their weaknesses: runtime cost, strict contract between caller and the callee and lack of type safety. Not to mention the problem of default type promotion!

Fortunately, standards committee thought this through and equipped us in C++11 with variadic templates. Let’s check them out.

Variadic functions – Part 1: va_args and friends

From time to time there is a need to write a function, that accepts an unspecified number of arguments. In C++ we have multiple ways of handling this depending on the context, use case and available language features. But the oldest and still most commonly used mechanism is the va_arg.

You think nothing can surprise you? Let’s bet.