C++ How to simply handle variable number of function arguments

This is probably the first of the posts, I am going to write primarily for C++ beginners.  Where this title “beginner” is a misnomer in the context of C++. Becoming a solid C++ beginner takes time and effort.

This is not a good situation. And to understand some of the reasons why this is so,  please read what theC++ creator (Bjarne Stroustrup) has written about endless language extending. I think a very important “message to all” called  “Remember the Vasa!“.

A variable number of arguments

is a name of c++ function capability to be declared to receive zero or more, or a variable number of, arguments.

This capability is inherited from C and exists as such for decades now.

Since C++11 this was better formalized and the standard C++ has been equipped to handle this in a bit more high-level way, with the help of templates too.

Since C++11, the variable set of arguments is called “parameter pack”. For a good level of detail about it please see here.

In this post, we are dealing with C++ function templates,  with the capability to receive any number of function arguments, so-called (scarily): Variadic function template.

Now I am sure since you are reading this you know already all this stuff. But you might think it is non-trivial to deal with C++ parameter packs and as a such, you are avoiding them or blindly copy-paste some “scary code” from Stack Overflow to use them.

Standard C++ parameter pack is easy to handle

I am sure you have seen many scary-looking articles and forum discussions about programming parameter packs. And perhaps you have decided to “deal with it later”.

There is no need for that. Standard C++  makes for easy programming of parameter packs. I have prepared two functions to show you how easy.

First here is the code. It is pretty well documented. To make this code work for you with no hassle, please install and use Visual Studio 2019 community edition.

Please note the above code will compile only if all the arguments are of the same type. For some use-cases that is a severe limitation. Thus in that scenario, we provide a solution by using std::tuple.

Being a “C++ beginner”, you are not actually such a beginner. And as a such, you do realize we have a standard console application above and you would know which headers to include and where to put those includes.

Please compile and follow, several times, the above code through a very good Visual Studio debugger. That is an indispensable tool when studying C++.


You perhaps know we are dealing here with two generic lambdas with a variable number of arguments.

This is just a “proof of concept” code. It is here to show you how to use standard C++ mechanisms and coding idioms to deal with “param packs” in an easy and standard fashion.

First, we deal with “param packs” where each of them is of the same type, like in the main() above.

auto init_list = args_initlist(1, 2, 3);

Peeking into the init_list the function we immediately spot the “wonder” line:

auto arg_list = { args ... };

In standard C++, this declaration automatically transforms param pack expansion { args ... } into the constructor call to the std::initializer_list<T>. C++ compiler effectively replaces that line with the proper declaration of the init list

std::initializer_list<int> arg_list{ 1,2,3 } ;

Init list has begin() and end() methods and can be used to reach parameter pack elements, and a such. No scary template programming. Thus you can use it in a C++ for range loop:

for ( auto  && element  : arg_list ) {
// do something with each element
// in this case do something with each argument of the
// param pack 

There are also a few type declarations in there you might find useful. Good, nice, simple …. But. What do we do if we need to pass a variable number of arguments but of a different type?

We will use C++ tuple.

Another function is dealing with “param pack” of a multitude of types. As you can see the key line is this one:

auto tup_list = make_tuple( args … );

Again we simply expand the param pack, but this time we use it to provide a variable number of arguments to pass to the std::make_tuple function.

In here you do whatever you need to do. You have all the arguments nicely handled as elements of the std::tuple.


The point is here to show how standard C++ can be really simple, but functional and powerful. Not just simple looking.

Standard C++ has many powerful tools to solve and program each development task. The trick is to know them all and select the best one. Which is almost always not the most complex one.

List of different types
List of different types
%d bloggers like this: