Variadic Templates

 · 2 min read
 · Arunabh Sharma
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
#include <iostream>

// Serves as the base case of the recursion when only one argument is left, 
// can also be a print statement with no arguments and a do nothing return
template<typename T>
void print(const T &value)
{
    std::cout << value << std::endl;
}
// Variadic template based print function
template<typename T, typename ... Args>
void print(const T& value, const Args&... args)
{
    std::cout << value << std::endl;
    print(args...);
}
int main()
{
    print(42, "answers are needed", 3.567);
    print(true, "chatgpt will make us all obsolete in the medium term");
    return 0;
}

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.