Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "type ... pack-name" parameter pack in C++?

Tags:

c++

templates

The cppreference page on parameter pack states there is a parameter pack like this:

type ... pack-name(optional)    (1)

But how do you use it?

This doesn't work and the error is syntactical:

template<int... Ints>
int sum_2_int(Ints... args)
{
    return (int)(args + ...);
}

I can't figure out how to use this thing from the description and I don't see an example of the usage anywhere on that page. I may have just skipped it because I am very inexperienced in this part of c++.

EDIT1: I am not trying to sum an arbitrary amount of integers or whatever types. I've written this function because of my complete lack of understanding of how and where to use this type of parameter pack since I assumed it will be similar to the type (2) typename|class ... pack-name(optional).

EDIT2: Now I know that trying to use Ints... args as a parameter in function definition is futile. I made a new snippet, that works now here. If you know more examples of the usage of this type of parameter pack, please share.

like image 310
a_girl Avatar asked Dec 22 '25 21:12

a_girl


1 Answers

So, what I've learned about type (1) parameter pack:

  1. It is INCORRECT to use as a parameter in the function definition, since template<int... Ints> int foo(...) {...} just means that you should later use your function as int i = foo<1,2,3,4,...>(...);
  2. It can be used as an argument to some function after the expansion happens:
// parameter pack type [2] typename|class ... pack-name(optional)
template <typename... Types>
int sum_to_int(Types... args)
{
    return (int)(args + ...); // fold expression used here
}

// parameter pack [1] type ... pack-name(optional)
template <int... Ints>
int sum_ints_statically()
{
    return sum_to_int(Ints...);
}

int main()
{
    return sum_ints_statically<1,2,3,4,5>(); // will return 15!
}

Thanks Evg and user17732522 for helping me to find the answer.

Please add more usage examples if you know more!

like image 99
a_girl Avatar answered Dec 24 '25 10:12

a_girl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!