Skip to content

Commit 436840d

Browse files
committed
Refine tuple index implementation for lint
1 parent fa8e96d commit 436840d

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

envpool/core/tuple_utils.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#ifndef ENVPOOL_CORE_TUPLE_UTILS_H_
1818
#define ENVPOOL_CORE_TUPLE_UTILS_H_
1919

20-
#include <array>
2120
#include <functional>
2221
#include <tuple>
2322
#include <type_traits>
@@ -29,16 +28,23 @@ struct Index;
2928

3029
template <class T, class... Types>
3130
struct Index<T, std::tuple<Types...>> {
32-
static constexpr std::size_t kValue = [] {
33-
constexpr std::array<bool, sizeof...(Types)> kMatches{
34-
std::is_same_v<T, Types>...};
35-
for (std::size_t i = 0; i < kMatches.size(); ++i) {
36-
if (kMatches[i]) {
37-
return i;
38-
}
31+
private:
32+
template <std::size_t I>
33+
static constexpr std::size_t FindIndex() {
34+
return I;
35+
}
36+
37+
template <std::size_t I, class Head, class... Tail>
38+
static constexpr std::size_t FindIndex() {
39+
if constexpr (std::is_same_v<T, Head>) {
40+
return I;
41+
} else {
42+
return FindIndex<I + 1, Tail...>();
3943
}
40-
return kMatches.size();
41-
}();
44+
}
45+
46+
public:
47+
static constexpr std::size_t kValue = FindIndex<0, Types...>();
4248
static_assert(kValue < sizeof...(Types), "Type not found in tuple");
4349
};
4450

0 commit comments

Comments
 (0)