Unpacking the Packs
But how can the function access the contents of these packs? There is no indexing feature. That is, you can’t use something like Args[2]
to access the third type in a pack. Instead, you can unpack the pack by placing the ellipsis to the right of the function parameter pack name. For example, consider the following flawed code:
template<typename... Args> // Args is a template parameter packvoid show_list1(Args... args) // args is a function parameter pack{ show_list1(args...); // passes unpacked args to show_list1()}
What does this mean, and why is it flawed? Suppose we have this function call:
show_list1(5,'L',0.5);
The call packs the values 5
, 'L'
, and 0.5
into args
. Within the function, the call
show_list1(args...); ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.