Expand the options package using a counter
I want to expand a parameter package into a function call, for example:
func(get_value(arg)...);
where func and get_value are two functions. The question is that get_value is order-sensitive, so I think one solution is to create something like this:
func(get_value(arg0, 0), get_value(arg1, 1), get_value(arg2, 2));
if, for example, there are three parameters. With this counter, I will know the order of evaluation. Is this the way to do it?
Or as another solution, is there a way to just specify the order in which these get_value calls with args are evaluated? (If so, I don't even need a counter.)
source to share
The zip parameter packages can be bundled together. So, something like this, using the definition seq
from here :
template <typename ... Args, int ... N>
void F(Args... args, seq<N...>)
{
func(get_value(args, N)...);
}
template <typename ... Args>
void FHelper(Args&&... args)
{
F(std::forward<Args>(args)..., typename gens<sizeof...(Args)>::type ());
}
and you would call FHelper
.
A simpler solution, in a case get_value
that has a fixed return type, would be to change func to take initializer_list
as input and call it like this:
func({get_value(args)...});
This preserves the order of the invocation, as the commas in the initializer list are sequence points.
source to share