File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
3029template <class T , class ... Types>
3130struct 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
You can’t perform that action at this time.
0 commit comments