The term “beginner” is a misnomer in the context of C++. Becoming a solid C++ beginner takes time and effort.
[Published on: 2 August 2018]
[detour]
This kind of article for apparent “C++ beginners” is required for a few various reasons, and that is not a good situation for C++. And to understand some of the reasons that got us in that situation, please read what the C++ creator (Bjarne Stroustrup) has written about endless language extending. I think a very important “message to all” C++ well-wishers under by now well-known name: “Remember the Vasa!“.
[/detour]
This article is one of my attempts to show you that C++ code can be simple code.
What is a variable number of arguments?
If and when the function can have “zero or more arguments” it is said to have a variable number of, arguments. Function well known to you can have one or more arguments. printf()
.
This capability is inherited from C and exists as such for decades now.
[Update 2023 JAN] For C aficionados here is a tasty little macro to help with the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include <stdarg.h> // this is not C++, this is C double average(int num,...) { va_list valist; double sum = 0.0; int i; /* initialize valist for num number of arguments */ va_start(valist, num); /* access all the arguments assigned to valist */ for (i = 0; i < num; i++) { sum += va_arg(valist, int); } /* clean memory reserved for valist */ va_end(valist); return sum/num; } int main(void) { printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5)); printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15)); } |
Since C++11
became a standard, this was better formalized and the standard C++
has been equipped since then, 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 a “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.
1 2 3 4 5 6 7 8 |
// declaration of a variadic function template template<class ... Types> void vf(Types ... args); // usage vf(); // OK: args contains no arguments vf(1); // OK: args contains one argument: int vf(2, 1.0); // OK: args contains two arguments: int and double |
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 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 unfriendly-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; I hope. To make this code work for you with no hassle, please install and use Visual Studio 2022 community edition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// in stdafx.h add all the includes necessary #include "stdafx.h" // beware of anonymous namespace in headers namespace { // lambda is the easiest for handling arguments param pack when all the // arguments are of the same type auto args_initlist = [] (auto && ... args) { // if no arguments sent // return empty result if constexpr(1 > (sizeof... (args))) { // C++ compiler "knows" the type // of the return value return{}; } else { using namespace std; // here we transform arguments parameter pack to init list // a feature also called: "list initialization" // https://en.cppreference.com/w/cpp/language/list_initialization // NOTE: since C++17 more than one element in the init list can be deduced // only by copy declaration, in C++11/14 // this is thus error -> auto arg_list{ args ... }; // and in C++17 this is not auto arg_list = { args ... }; // the type of the init list instance using arg_list_type = decltype(arg_list); // the type of the elements in the init list instance using arg_list_value_type = typename arg_list_type::value_type; /* init list is a kind of a range so we can handle its elements in an range for loop for (element && : arg_list ) { ... do something with element ... } */ return arg_list; } }; // end of variable initialization with lambda needs an semi colon |
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 using std::tuple
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// handling arguments param pack when // arguments are of different types // template< typename ... Args > auto args_tuple = [] (auto && ... args) { if constexpr(1 > (sizeof... (args))) { // no arguments given // return empty tuple return{}; } using namespace std; // here we transform arguments parameter pack to tuple // <a href="https://en.cppreference.com/w/cpp/utility/tuple/make_tuple">std::make_tuple - cppreference.com</a> auto tup_list = make_tuple( args ... ); // the type of the whole tuple instance // as an added bonus using arg_list_type = decltype(tup_list); /* tuple is not a range see here how to get to the tuple elements https://en.cppreference.com/w/cpp/utility/tuple/get */ return tup_list; }; // eof lambda } // eof namespace int main () { // use the first variadic function template // with arguments of a single type auto init_list = args_initlist(1, 2, 3); // use the second variadic function template // with arguments of any type auto tup_ = args_tuple(1, 2.34, true, L"Abra", " Ka", " Dabra!"); // tuple can be easily decomposed with // "structured binding" into separate variables each of // required type auto [a, b, c, d, e, f] = tup_; } |
Being a “C++ beginner”, you are not actually such a beginner. And as 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; you have installed it as instructed. That is an indispensable tool when studying C++.
Discussion
You perhaps know we are dealing here with two generic lambdas with a variable number of arguments.
1 2 3 4 5 6 7 8 9 |
// receive param pack // each argument must be of the same type // return std::initializer_list<T> auto args_initlist = [] (auto && ... args); // receive param pack // arguments can be of different types // return std::tuple< class ... Types> auto args_tuple = [] (auto && ... args); |
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, as in the main()
above.
auto init_list = args_initlist(1, 2, 3);
Peeking into that function we immediately spot the “wonder” line:
auto arg_list = { args ... };
In standard C++ (17 and beyond), 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 that can be used to reach parameter pack elements. No scary template programming. The compiler uses them in a C++ for range loop:
1 2 3 4 5 6 |
for ( auto && element : arg_list ) { // do something with each element // in this case do something with each argument of the // param pack , for example std::cout << element ; } |
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 the “param pack” of a multitude of types. As you can see the key line is now this one:
1 |
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.
Conclusion
The point is here to show how standard C++ can be really simple, but functional and powerful. Not just scary-looking code.
Standard C++ has many powerful tools to solve a development task. The trick is to know them all and select the best one. Which is almost always never the most complex one.