0

I have a template parameter pack that I would like to pass into a function that takes in a variable number of parameters. Since I don't know how many parameters I actually have, how can I pass in an unknown amount of parameters' data members into the function?

template <typename BarType...>
Class Test
{
   explicit Test(BarType ...bars)
   {
   // Calling this function with parameter pack instead of this way 
   // (which won't even work with above parameter pack)
   FooFunction(
   [](BarType &bar1){ return bar1.bar_member; },
   [](BarType &bar2){ return bar2.bar_member; },
   [](BarType &bar3){ return bar3.bar_member; })
   }

}

How can I accomplish the above FooFunction without manually listing out all three parameters (making it more dynamic)? Since there might be more parameters bar4, bar5 etc. So just manually listing them won't work.

6
  • 1
    Your description is slightly confusing. In C++, there are no functions that "takes in a variable amount of parameters". Templates do something like that, but templates are not functions. The shown code example does not "pass a data member of BarType to Function", but passes three closures to the function, each one of which does exactly the same thing (yes, they really do, they differ only in names of parameters, which means absolutely nothing, in actual C++ code). Your question is hard to understand. Please rephrase it, and provide a real code example, instead of "Foo"-this, or "Bar". Commented Mar 27, 2020 at 0:27
  • Why does FooFunction(bar1, bar2, bar3) not work for you? Commented Mar 27, 2020 at 0:29
  • @nick just doing that won't work because I have a variadic list of parameters. I am writing that out as an example but I might have more parameters. I know I can somehow "unpack" the variadic parameters but I don't know how to accomplish that Commented Mar 27, 2020 at 1:00
  • Can you show code explaining what you mean by "I have a variadic list of parameters"? Commented Mar 27, 2020 at 1:02
  • Oh, I think I understand now what you're asking. Take a look at this: Unpacking parameter pack from template arguments Commented Mar 27, 2020 at 1:16

1 Answer 1

1

It looks like you might want:

template <typename BarType...>
Class Test
{
   explicit Test(BarType ...bars)
   {
       FooFunction(bars.bar_member ...);
   }
};

The most common way of using a parameter pack is to expand it, by putting a representation of what should be in each element of a list, followed by the ellipsis token ....

So if we take the specialization Test<B1, B2, B3>, it has a constructor like explicit Test(B1 b1, B2 b2, B3 b3); except that the names b1, b2, and b3 are just made up for explanation. In that function body, some possible things involving pack expansions and what they would instantiate as:

std::make_tuple(bars...) -> std::make_tuple(b1, b2, b3)
std::tuple<BarType...>   -> std::tuple<B1, B2, B3>
f(g(bars) ...)           -> f(g(b1), g(b2), g(b3))
h<BarType>(bars)...      -> h<B1>(b1), h<B2>(b2), h<B3>(b3)

The last one expands both the template parameter pack BarType and the function parameter pack bars together, matching up the corresponding pack elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.