You can use variadic template arguments and tuples:
#include
#include
template
void f(Args&&... args)
{
auto arguments = std::make_tuple(std::forward(args)...);
std::cout << std::get<0>(arguments);
}
void f() {} // for 0 arguments
int main()
{
f(2, 4, 6, 8);
}
Live Demo
For bounds checking, try the following:
#include
#include
template
struct input
{
std::tuple var;
input(T&&... t) : var(std::forward(t)...) {}
template ::value>
auto get() -> typename std::tuple_element>::type
{
return std::get(var);
}
};
template
void f(Args&&... args)
{
auto arguments = input(std::forward(args)...);
std::cout << arguments.template get<9>();
}
void f() {} // for 0 arguments
int main()
{
f(2, 4, 6, 8);
}
Update: If you need the first argument then you simply want a function which exposes that first argument by separating it from the parameter pack:
template
void foo(Head&& head, Tail&&... tail);
If this is not satisfactory (i.e you want to get the nth-argument), you can unpack the arguments into a std::tuple<> and retrieve an element with std::get<>:
template
void foo(Args&&... args)
{
auto t = std::forward_as_tuple(std::forward(args)...);
print(std::get<5>(t));
}