Skip to content

Instantly share code, notes, and snippets.

@tamaskenez
Created November 29, 2022 17:41
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 tamaskenez/ad1062aeda3a050d1de284ce3686b8db to your computer and use it in GitHub Desktop.
Save tamaskenez/ad1062aeda3a050d1de284ce3686b8db to your computer and use it in GitHub Desktop.
more readable macro to replace std::holds_alternative
/ 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