Variadic template, get function arguments value
c++, c++11, variadic-functions, variadic-templates
Solution
Capture the args in a temporary tuple (Live at Coliru):
ReturnType operator()(Args... args)
{
static_assert(sizeof...(args) >= 3, "Uh-oh, too few args.");
// Capture args in a tuple
auto&& t = std::forward_as_tuple(args...);
// Get argument 0
std::cout << std::get<0>(t) << '\n';
// Get argument 1
std::cout << std::get<1>(t) << '\n';
// Get argument 2
std::cout << std::get<2>(t) << '\n';
}
`std::forward_as_tuple` uses perfect forwarding to capture references to the `args`, so there should be no copying.
Problem
My problem is the following: I have a class declared as such: ``` template<typename ReturnType, typename... Args> class API { ReturnType operator()(Args... args) { // Get argument 0 // Get argument 1 } }; ``` I am in need of getting the arguments on by one, and so far the only way I've come up to (but I can not get it to work) is using `std::get`, as such: ``` std::get<0>(args); ``` Of course, this leads to a lot of errors. I am new to variadic templates (and to C++11 at all) so I am quite lost at this point. How could I get those arguments one by one? Any help will be appreciated.