Last active
March 7, 2025 00:56
-
-
Save DrkWithT/bcd61292c90fcc936df0ffba7ac481c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <type_traits> | |
#include <iostream> | |
struct Foo {}; | |
template <typename... Args> | |
struct TypeList {}; | |
template <int N, typename Target> | |
constexpr int searchTypeImpl() { | |
return -1; | |
} | |
template <int N, typename Target, typename Next, typename... Rest> | |
constexpr int searchTypeImpl() { | |
if constexpr (std::is_same_v<Target, Next>) { | |
return N; | |
} | |
return searchTypeImpl<N + 1, Target, Rest...>(); | |
} | |
template <typename Target, template<typename...> typename TList, typename... Args> | |
constexpr int findType([[maybe_unused]] TList<Args...> arg) { | |
return searchTypeImpl<0, Target, Args...>(); | |
} | |
int main() { | |
TypeList<bool, char, int, long> integrals; | |
static_assert(findType<int>(integrals) == 2, "Unexpected index of int type!"); | |
static_assert(findType<Foo>(integrals) == -1, "Unexpected index of non-included type!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: add a check for empty type sequences in
findType()
.