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...); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access