I needed to expand a tuple so that I can apply it's argument to a function. It was surprisingly easy to write.
template<template<class...> class Tup, class... Ts, class F>
decltype(auto) apply(F f, Tup<Ts...> &tup){
return f(std::forward<Ts>(std::get<Ts>(tup))...);
}
And it can be used like this
auto it = std::make_tuple(1,2);
cout << apply([](int a, int b){return a + b;},it) << endl;
But what if I actually want to move the elements of the tuple into the function?
So I tried something more general
template<template<class...> class Tup, class... Ts, class F>
decltype(auto) apply(F f, Tup<Ts...> &&tup){
return f(std::forward<Ts>(std::get<Ts>(tup))...);
}
but then it only works with rvalues
auto it = std::make_tuple(1,2);
cout << apply([](int a, int b){return a + b;},std::move(it)) << endl;
For lvalue references I get
error: no matching function for call to 'apply'
cout << apply([](int a, int b){return a + b;},it) << endl;
^~~~~
What am I doing wrong?
Edit: It might not be the best example because I am using ints which will always be copied.