// implement 1
template <typename Tuple, typename Func, size_t Index = 0>
void tuple_for_each(const Tuple& t, const Func& f) {
constexpr auto n = std::tuple_size_v<Tuple>;
if constexpr (Index < n) {
tuple_at<Index>(t, f);
tuple_for_each<Tuple, Func, Index + 1>(t, f);
}
}
// implement 2
template <typename Function>
void for_each(const std::tuple<>&, Function) { }
template <typename T, typename... Ts, typename Function>
void for_each(const std::tuple<T, Ts...>& tuple, Function func) {
func(std::get<0>(tuple));
for_each(std::tuple<Ts...>(std::get<Ts>(tuple)...), func);
}
第一种用到 size_t Index = 0,惊呼了一下这么玩
--
FROM 113.91.210.*