Created
November 29, 2022 17:41
-
-
Save tamaskenez/ad1062aeda3a050d1de284ce3686b8db to your computer and use it in GitHub Desktop.
more readable macro to replace std::holds_alternative
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
/ IS_ / IS_NOT_ replacement of std::holds_alternative | |
// Inspiration: https://youtu.be/raB_289NxBk?t=1088 | |
// Usage: | |
// | |
// if (someVariantVar IS_(AlternativeType)) { ... | |
// if (someVariantVar IS_NOT_(AlternativeType)) { ... | |
// | |
// Precedence: same as binary * | |
// | |
#define IS_(TYPE) *::clai::IsInitiator<TYPE, false>() | |
#define IS_NOT_(TYPE) *::clai::IsInitiator<TYPE, true>() | |
template <typename T, bool Invert> | |
class IsInitiator {}; | |
template <typename T, bool Invert, typename... Types> | |
constexpr bool operator*(const std::variant<Types...>& v, | |
const IsInitiator<T, Invert>&) noexcept { | |
return std::holds_alternative<T>(v) ^ Invert; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment