namespace details {
template <std::size_t I = 0, typename Tuple, typename Function, typename... Args>
typename std::enable_if<I == std::tuple_size<Tuple>::value, void>::type ForEach(Tuple &t, Function f, Args &... args) {}
template <std::size_t I = 0, typename Tuple, typename Function, typename... Args>
typename std::enable_if<(I < std::tuple_size<Tuple>::value), void>::type ForEach(Tuple &t, Function f, Args &... args) {
f(std::get<I>(t), args...);
ForEach<I + 1>(t, f, args...);
}
}
An implementation for ForEach functionality for all types of a tuple is above. It calls f(tuple_type, args...)
However I want something like tuple_type.f(args...)
where f and args are template arguments.
f would be a member function of all the types in the tuple, taking args... as arguments.
template <typename... Types>
class TupleManager {
std::tuple<Types...> t;
template <typename Function, typename... Args>
void ForEach(Function f, Args& ... args) {
details::ForEach<>(t, f, args...);
}
}
Clarification: f needs to be a member function, i.e a function that takes the same name for all types in the tuple.
Eg:
struct A {
void foo() {
std::cout << "A's foo\n";
}
};
struct B : A {
void foo() {
std::cout << "B's foo\n";
}
};
struct C : A {
void foo() {
std::cout << "C's foo\n";
}
};
But now I can't pass foo
. Passing &A::foo
print's A's foo. The requirement is to print A'foo for object of A in the tuple, B's foo for object of B in the tuple, and C's foo for object of C in the tuple.
f
to be betweentuple_type
andargs...
? You want to use exactly that syntax, wheref
happens to be the name of a common member function? You want exactly that punctuation? – WiddershinsForEach(myTuple, &C::foo)
... – Toxoidf(std::get<I>(t), args...);
tostd::get<I>(t).f(args...)
and see Jarod's comment. – Howze