Skip to content

Instantly share code, notes, and snippets.

@willeccles
Created June 22, 2023 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willeccles/4c72891766f31c7b32e645c0f1968df6 to your computer and use it in GitHub Desktop.
Save willeccles/4c72891766f31c7b32e645c0f1968df6 to your computer and use it in GitHub Desktop.
#ifndef ONE_OF_H
#define ONE_OF_H
#include <tuple>
#include <utility>
template <class... T>
struct one_of {
std::tuple<T...> t;
constexpr one_of(T&&... values) : t{std::forward<T>(values)...} {}
template <class U>
constexpr bool operator==(U&& v) const&& {
return comp_impl(std::forward<U>(v), t,
std::make_index_sequence<sizeof...(T)>{});
}
private:
template <class U, class Tup, std::size_t... I>
constexpr inline static bool comp_impl(U&& v, const Tup& tup,
std::index_sequence<I...>) {
return ((std::forward<U>(v) == std::get<I>(tup)) || ...);
}
};
#endif // ONE_OF_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment