Variadic Templates
Table of contents
Intuition
If you want to write code that can generalize not just across the type but also the number of arguments, you can use variadic templates which serves as a generalization of regular templates
Explanation
Variadic Templates
Now generalizing above syntax to number of arguments also, the way to represent a variable number of arguments is the ellipsis operator ..., this variadic argument is then processed recursively until all arguments have been processed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Application
This concept can be used to implement a logging class by wrapping around an existing log library like the above print function and publish to an object like std::ostringstream
or directly to console.
Why is this relevant?
When the constructor or make_xxxxx() functions are called, the number of arguments vary based on the constructor of the template class for which the smart pointer is instantiated.