1
template<class... Ts, class... Ps>
void Fun(Ts... ts, Ps... ps){};

Fun(1,'a',"blah", 2.13, 3.14f); 
// how do we separate which part is for ts or ps params pack

is there a way to explicit annotate which part of params is belongs to ts/ps variadic params?

Larry
  • 155
  • 7
  • 1
    You need a helper class to deliniate the packs. See [here](https://stackoverflow.com/questions/9831501/how-can-i-have-multiple-parameter-packs-in-a-variadic-template) for an example. – rustyx May 21 '20 at 09:42
  • Does this answer your question? [How can I have multiple parameter packs in a variadic template?](https://stackoverflow.com/questions/9831501/how-can-i-have-multiple-parameter-packs-in-a-variadic-template) – erenon May 21 '20 at 09:44

2 Answers2

0

No, variadic template packs are greedy and you can not put them next to each other.

You could wrap your packs in tuples and pass them that way.

#include <tuple>

template<class... Ts, class... Ps>
void Fun(std::tuple<Ts...> ts, std::tuple<Ps...> ps) {
    // Do something with the tuples
}

int main()
{
   Fun(std::make_tuple(1,'a',"blah"), std::make_tuple(2.13, 3.14f)); 
}
super
  • 12,335
  • 2
  • 19
  • 29
0

how do we separate which part is for ts or ps params pack

More straightforward would be to rewrite the function to take 2 tuples.

But notice that you can already split both parameter packs.

Ts is non deducible, and Ps takes so all the remaining for deduction,
so by explicitly give Ts arguments, you split them:

Fun(1, 'a', "blah", 2.13, 3.14f);            // Ts: []; Ps: [int, char, const char*, double, float]
Fun<int, char>(1, 'a', "blah", 2.13, 3.14f); // Ts: [int, char]; Ps: [const char*, double, float]

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302