Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <type_traits>
  2.  
  3. enum class a_t { a1, a2, a3, a4 };
  4. enum class b_t { };
  5. enum class c_t { };
  6.  
  7. // Make a collection of types we want to define an operator| for.
  8. enum typename or_types_t {
  9.   a_t, c_t
  10. };
  11.  
  12. // Loop over each enumerator in or_types_t.
  13. @meta for enum(auto e : or_types_t) {
  14.   // Define the function that takes two parameters with the associated type
  15.   // and returns a result of the associated type.
  16.   @enum_type(e) operator|(@enum_type(e) lhs, @enum_type(e) rhs) {
  17.     typedef @enum_type(e) type_t;
  18.     static_assert(std::is_enum<type_t>::value);
  19.  
  20.     typedef std::underlying_type<type_t>::type underlying_t;
  21.  
  22.     return (type_t)((underlying_t)lhs | (underlying_t)rhs);
  23.   }
  24. }
  25.  
  26. int main() {
  27.   a_t a = a_t::a2 | a_t::a4;
  28.   return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement