Skip to content

Instantly share code, notes, and snippets.

@DrkWithT
Last active March 7, 2025 00:56
Show Gist options
  • Save DrkWithT/bcd61292c90fcc936df0ffba7ac481c3 to your computer and use it in GitHub Desktop.
Save DrkWithT/bcd61292c90fcc936df0ffba7ac481c3 to your computer and use it in GitHub Desktop.
#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!");
}
@DrkWithT
Copy link
Author

DrkWithT commented Mar 7, 2025

TODO: add a check for empty type sequences in findType().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment