Last active
July 10, 2020 08:37
-
-
Save picanumber/788ac0163f74c1df59dcf579bedaebd0 to your computer and use it in GitHub Desktop.
In place method to find the index of a type in a tuple
This file contains 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
template <class T, std::size_t I, class Tuple> // 1 | |
constexpr bool match_v = std::is_same_v<T, std::tuple_element_t<I, Tuple>>; | |
template <class T, class Tuple, // 2 | |
class Idxs = std::make_index_sequence<std::tuple_size_v<Tuple>>> | |
struct type_index; | |
template <class T, template <class...> class Tuple, class... Args, // 3 | |
std::size_t... Is> | |
struct type_index<T, Tuple<Args...>, std::index_sequence<Is...>> | |
: std::integral_constant<std::size_t, | |
((Is * match_v<T, Is, Tuple<Args...>>) + ... + 0)> { | |
static_assert(1 == (match_v<T, Is, Tuple<Args...>> + ... + 0), | |
"T doesn't appear once in Tuple"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment