Created
June 22, 2023 15:37
-
-
Save willeccles/4c72891766f31c7b32e645c0f1968df6 to your computer and use it in GitHub Desktop.
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
#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