Modern C++: Variadic Templates
dev.to·12w·
Discuss: DEV

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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help