Modern C++: Variadic Templates
dev.to·20h·
Discuss: DEV
Flag this post

This follows my series to cover lesser known features of Modern C++

Today, we have variadic templates, which is an improvement over variadic arguments (...), a type of argument that can take variable arguments.

Before we start with variadic templates, an introduction to variadic arguments is required.

Variadic Arguments

Variadic arguments have been around since the time of C and old C++ but it has a lot of limitations, which can be seen in the code demonstration below:

#include <iostream>
#include <cstdarg>

void printNumbers(int count, ...) {
va_list args; // Hold and make argument list with capacity "count"
va_start(args, count); // Start processing variable args

for (int i = 0; i < count; ++i) {
int num = va_arg(args, int); // Get next argument
std::cout...

Similar Posts

Loading similar posts...