c++ - Is it possible to call a member function for every type in a tuple? -
namespace details { template <std::size_t = 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 = 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 foreach functionality types of tuple above. calls f(tuple_type, args...)
however want tuple_type.f(args...) f , args template arguments.
f member function of types in tuple, taking args... 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 member function, i.e function takes same name types in tuple.
eg:
struct { void foo() { std::cout << "a's foo\n"; } }; struct b : { void foo() { std::cout << "b's foo\n"; } }; struct c : { void foo() { std::cout << "c's foo\n"; } }; but can't pass foo. passing &a::foo print's a's foo. requirement print a'foo object of in tuple, b's foo object of b in tuple, , c's foo object of c in tuple.
Comments
Post a Comment