From the course: C++ Best Practices for Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

The magic of variadic template functions

The magic of variadic template functions - C++ Tutorial

From the course: C++ Best Practices for Developers

Start my 1-month free trial

The magic of variadic template functions

- [Instructor] C plus plus variadic functions are a C holdover. They have always been a bit problematic to use, since there's no checking of the past argument types. Variadic template functions use generic templates to make variadic argument passing cleaner, and it is still able to work with a heterogeneous set of arguments. Let's take a look at an example. In this bit of code, we have to find a variadic template function name rcatenate. It takes the variable number of standard strings, notice that I'm saying standard strings, not C strings, and reverse concatenates them with spaces in between the words. By the way, this is just a bit of sample code. It is definitely not ready for prime time. The args dot dot dot args is the parameter pack. The way it works is pretty cool. It essentially means the rest of the arguments. So, in our example, when we call with three arguments, first, the first one is assigned a first, and the remaining two are assigned to args. Then when we do our…

Contents