Last active
August 29, 2015 14:07
-
-
Save dgodfrey206/be67ec341a0794e19b25 to your computer and use it in GitHub Desktop.
The second improved version of has_type. It's shorter, does away with recursion and uses the bool_sequence idiom.
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 <tuple> | |
template <bool...> | |
struct bool_sequence { }; | |
template <bool... Bs> | |
using bool_and = std::is_same<bool_sequence<Bs...>, | |
bool_sequence<(Bs || true)...>>; | |
template <bool... Bs> | |
using bool_or = std::integral_constant<bool, !bool_and<!Bs...>::value>; | |
namespace detail | |
{ | |
template <typename value, typename tuple> | |
struct has_type; | |
template <typename value, typename... xs> | |
struct has_type<value, std::tuple<xs...>> | |
{ | |
using type = bool_or<std::is_same<xs, value>::value...>; | |
}; | |
} | |
template <typename value, typename tuple> | |
using has_type_t = typename detail::has_type<value, std::decay_t<tuple>>::type; | |
template <typename value, typename tuple> | |
constexpr auto has_type(tuple&&) -> has_type_t<value, tuple> | |
{ | |
return {}; | |
} | |
int main() | |
{ | |
constexpr std::tuple<bool, double, int, short> t; | |
static_assert(has_type<int>(t), ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment