Skip to content

Instantly share code, notes, and snippets.

@Pikachuxxxx
Created November 28, 2025 02:24
Show Gist options
  • Select an option

  • Save Pikachuxxxx/6b2739720bf22335ad2a4d75e8d010c8 to your computer and use it in GitHub Desktop.

Select an option

Save Pikachuxxxx/6b2739720bf22335ad2a4d75e8d010c8 to your computer and use it in GitHub Desktop.
enum class explicit operators
#define DECLARE_ENUM_BITWISE_OPERATORS(enum_type) \
inline constexpr enum_type operator |(enum_type lhs, enum_type rhs) { return static_cast<enum_type> ( static_cast<std::underlying_type<enum_type>::type>(lhs) | static_cast<std::underlying_type<enum_type>::type>(rhs) ); } \
inline constexpr enum_type operator &(enum_type lhs, enum_type rhs) { return static_cast<enum_type> ( static_cast<std::underlying_type<enum_type>::type>(lhs) & static_cast<std::underlying_type<enum_type>::type>(rhs) ); } \
inline constexpr enum_type operator ^(enum_type lhs, enum_type rhs) { return static_cast<enum_type> ( static_cast<std::underlying_type<enum_type>::type>(lhs) ^ static_cast<std::underlying_type<enum_type>::type>(rhs) ); } \
inline constexpr enum_type operator ~(enum_type lhs) { return static_cast<enum_type> ( ~static_cast<std::underlying_type<enum_type>::type>(lhs) ); } \
inline constexpr enum_type operator |=(enum_type& lhs, enum_type rhs) { lhs = static_cast<enum_type> ( static_cast<std::underlying_type<enum_type>::type>(lhs) | static_cast<std::underlying_type<enum_type>::type>(rhs) ); return lhs; } \
inline constexpr enum_type operator &=(enum_type& lhs, enum_type rhs) { lhs = static_cast<enum_type> ( static_cast<std::underlying_type<enum_type>::type>(lhs) & static_cast<std::underlying_type<enum_type>::type>(rhs) ); return lhs; } \
inline constexpr enum_type operator ^=(enum_type& lhs, enum_type rhs) { lhs = static_cast<enum_type> ( static_cast<std::underlying_type<enum_type>::type>(lhs) ^ static_cast<std::underlying_type<enum_type>::type>(rhs) ); return lhs; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment