Skip to content

Instantly share code, notes, and snippets.

@kachsheev
Created October 10, 2019 17:04
Show Gist options
  • Save kachsheev/aeeb11139ee7b69f0275c88439bf327a to your computer and use it in GitHub Desktop.
Save kachsheev/aeeb11139ee7b69f0275c88439bf327a to your computer and use it in GitHub Desktop.
#include <utility>
namespace stdext
{
namespace details
{
template<typename T, T... elems>
struct is_element_of_sequence_impl
{
static inline bool _do(const T& value, std::integer_sequence<T, elems...>) noexcept;
};
template<typename T, T elem, T... elems>
struct is_element_of_sequence_impl<T, elem, elems...>
{
static inline bool _do(const T& value, std::integer_sequence<T, elem, elems...>) noexcept
{
if (elem != value)
{
using seq = std::integer_sequence<T, elems...>;
return is_elemenet_of_sequence_impl<T, elems...>::_do(value, seq{});
}
return true;
}
};
template<typename T>
struct is_element_of_sequence_impl<T>
{
static inline bool _do(const T& value, std::integer_sequence<T>)
{
return false;
}
};
} // namespace details
template<typename T, T... elems>
bool is_element_of_sequence(const T& t, std::integer_sequence<T, elems...>)
{
using seq = std::integer_sequence<T, elems...>;
return details::is_element_of_sequence_impl<T, elems...>::_do(t, seq{});
}
} // namespace stdext
// Example
std::integer_sequence<int, 1, 2, 3, 4, 5> seq;
int i1 = 3;
int i2 = 10;
stdext::is_element_of_sequence(i1, seq); // return true
stdext::is_element_of_sequence(i2, seq); // return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment