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 - C++ Tutorial
From the course: C++ Best Practices for Developers
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Auto type deduction and initialization1m 27s
-
(Locked)
Range-based for loops1m 45s
-
(Locked)
Strongly typed enums1m 58s
-
(Locked)
Using lambdas2m 39s
-
(Locked)
The magic of variadic template functions2m 12s
-
(Locked)
Challenge: Writing an integer sum method21s
-
(Locked)
Solution: Writing an integer sum method2m
-
(Locked)
-
-
-