One solution is provided by @Jonathan Wakely which employs the use of has_XXX
meta-functions.
Here is another solution, but it requires you to change your full-fledge structs definitions to mere typedefs of std::tuple<>
.
full-fledge structs:
struct case_0 { const char *foo; };
struct case_1i { const char *foo; int v0; };
struct case_1d { const char *foo; double v0; };
struct case_2ii { const char *foo; int v0; int v1; };
struct case_2id { const char *foo; int v0; double v1; };
are replaced with typedefs structs as follows:
typedef std::tuple<const char*> case_0;
typedef std::tuple<const char*,int> case_1i;
typedef std::tuple<const char*,double> case_1d;
typedef std::tuple<const char*,int,int> case_2ii;
typedef std::tuple<const char*,int,double> case_2id;
template<typename...Args>
auto foo(std::tuple<Args...> & tpl) -> decltype(std::get<0>(tpl))&
{
return std::get<0>(tpl);
}
template<typename...Args>
auto v0(std::tuple<Args...> & tpl) -> decltype(std::get<1>(tpl))&
{
return std::get<1>(tpl);
}
template<typename...Args>
auto v1(std::tuple<Args...> & tpl) -> decltype(std::get<2>(tpl))&
{
return std::get<2>(tpl);
}
and the usage
case_1i obj; //full-fledge struct
obj.foo = "hello";
obj.v0 = 100;
is replaced with
case_1i obj; //typedef struct
foo(obj) = "hello";
v0(obj) = 100;
Once you accept this design change, the solution to your original problem becomes pretty much straight-forward as follows:
template<size_t...>
struct seq{};
template<size_t M, size_t ...N>
struct genseq : genseq<M-1,M-1, N...> {};
template<size_t ...N>
struct genseq<0,N...>
{
typedef seq<N...> type;
};
template <typename ...Args, size_t ...N>
void call_do_stuff_with(std::tuple<Args...> & tpl, seq<N...>)
{
do_stuff_with(std::get<N>(tpl)...);
}
template <typename ...Args>
void process(std::tuple<Args...> & tpl)
{
const size_t N = sizeof ...(Args);
call_do_stuff_with(tpl, typename genseq<N>::type());
}
Do let me know if that is acceptable. If that is not acceptable, I will delete my answer (if you feel so).
Live demo!