Skip to content

Commit 4fb48e5

Browse files
PragmaTwicetqchen
authored andcommitted
[FFI] Use fold expression to simplify for_each (apache#18116)
1 parent ae51ded commit 4fb48e5

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

include/tvm/ffi/base_details.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,16 @@ namespace ffi {
136136
namespace details {
137137

138138
// for each iterator
139-
template <bool stop, std::size_t I, typename F>
140139
struct for_each_dispatcher {
141-
template <typename T, typename... Args>
142-
static void run(const F& f, T&& value, Args&&... args) { // NOLINT(*)
143-
f(I, std::forward<T>(value));
144-
for_each_dispatcher<sizeof...(Args) == 0, (I + 1), F>::run(f, std::forward<Args>(args)...);
140+
template <typename F, typename... Args, size_t... I>
141+
static void run(std::index_sequence<I...>, const F& f, Args&&... args) { // NOLINT(*)
142+
(f(I, std::forward<Args>(args)), ...);
145143
}
146144
};
147145

148-
template <std::size_t I, typename F>
149-
struct for_each_dispatcher<true, I, F> {
150-
static void run(const F&) {} // NOLINT(*)
151-
};
152-
153146
template <typename F, typename... Args>
154147
void for_each(const F& f, Args&&... args) { // NOLINT(*)
155-
for_each_dispatcher<sizeof...(Args) == 0, 0, F>::run(f, std::forward<Args>(args)...);
148+
for_each_dispatcher::run(std::index_sequence_for<Args...>{}, f, std::forward<Args>(args)...);
156149
}
157150

158151
/*!

0 commit comments

Comments
 (0)