Skip to content

Instantly share code, notes, and snippets.

@hutorny
Created March 11, 2019 09:11
Show Gist options
  • Save hutorny/714f7e67431b8523f77983707437e47c to your computer and use it in GitHub Desktop.
Save hutorny/714f7e67431b8523f77983707437e47c to your computer and use it in GitHub Desktop.
/* constexpr any_of against a list of constants */
/** type_of_list<...>::type - helper structure for determining type of the first item in the list */
template<auto ... List> struct type_of_list;
template<auto List> struct type_of_list<List> { using type = decltype(List); };
template<auto First, auto ... List > struct type_of_list<First, List...> : type_of_list<First> {};
/** type_of<...>::type - determines type of the first item in the list */
template<auto ... List>
using type_of = typename type_of_list<List...>::type;
/** any_of<...>(value) - compares value against all template parameters and returns true if any matches */
template<auto ... List>
inline constexpr bool any_of(type_of<List...> value) noexcept {
return ((value == List) || ...);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment