As an answer to your updated question:
as discussed in @ DavidRodríguez, get<0>
not enough, and not syntactically correct &get<0>
. You need &get<0,int,int>
. Following your example, this will be:
#include <tuple> using namespace std; template<typename T, typename U> U call (T x, U (*f)(T&) ) { return (*f)(x); }; int main() { tuple<int,int> tpl(42,43); call(tpl, &get<0,int,int>); return 0; }
In normal use, std::get<>()
int,int
is automatically output. But in your situation you need to provide it, since there are no parameters. One way is to use the get
template custom function:
#include <tuple> using namespace std; template <size_t I, typename T> auto myGet(T& tpl) -> decltype(get<I>(tpl)) { return get<I>(tpl); } template<typename T, typename U> U call (T x, U (*f)(T&) ) { return (*f)(x); }; int main() { tuple<int,int> tpl(42,43); auto get0 = &myGet<0, decltype(tpl)>; call(tpl, get0); // call(tpl, &myGet<0, decltype(tpl)>); // all in one line, do not work return 0; }
source share