Hi,
I have a function named _push
which can handle different parameters, including tuples, and is supposed to return the number of pushed elements.
For example, _push(5)
should push '5' on the stack (the stack of lua) and return 1 (because one value was pushed), while _push(std::make_tuple(5, "hello"))
should push '5' and 'hello' and return 2.
I can't simply replace it by _push(5, "hello")
because I sometimes use _push(foo())
and I want to allow foo()
to return a tuple.
Anyway I can't manage to make it work with tuples:
template<typename... Args, int N = sizeof...(Args)>
int _push(const std::tuple<Args...>& t, typename std::enable_if<(N >= 1)>::type* = nullptr) {
return _push<Args...,N-1>(t) + _push(std::get<N-1>(t));
}
template<typename... Args, int N = sizeof...(Args)>
int _push(const std::tuple<Args...>& t, typename std::enable_if<(N == 0)>::type* = nullptr) {
return 0;
}
Let's say you want to push a tuple<int,bool>
. This is how I expect it to work:
_push<{int,bool}, 2>
is called (first definition)_push<{int,bool}, 1>
is called (first definition)_push<{int,bool}, 0>
is called (second definition)
However with g++ 4.5 (the only compiler I have which supports variadic templates), I get an error concerning _push<Args...,N-1>(t)
(line 3) saying that it couldn't find a matching function to call (without any further detail). I tried without the "..." but I get another error saying that the parameters pack is not expanded.
How can I fix this?
PS: I know that you can do this using a template struct (this is in fact what I was doing before), but I'd like to know how to do it with a function
PS 2: PS2 is solved, thanks GMan