fork download
  1. #include <type_traits>
  2. #include <bitset>
  3. #include <iostream>
  4.  
  5. template< typename >
  6. struct enum_traits;
  7.  
  8. // Lots of components
  9. enum class components {
  10. comp_01, comp_02, comp_03, comp_04, comp_05, comp_06, comp_07, comp_08, comp_09, comp_10,
  11. comp_11, comp_12, comp_13, comp_14, comp_15, comp_16, comp_17, comp_18, comp_19, comp_20,
  12. comp_21, comp_22, comp_23, comp_24, comp_25, comp_26, comp_27, comp_28, comp_29, comp_30,
  13. comp_31, comp_32, comp_33, comp_34, comp_35, comp_36, comp_37, comp_38, comp_39, comp_40,
  14. comp_41, comp_42, comp_43, comp_44, comp_45, comp_46, comp_47, comp_48, comp_49, comp_50,
  15. comp_51, comp_52, comp_53, comp_54, comp_55, comp_56, comp_57, comp_58, comp_59, comp_60,
  16. comp_61, comp_62, comp_63, comp_64, comp_65, comp_66, comp_67, comp_68, comp_69, comp_70,
  17. count // size of enumeration
  18. };
  19.  
  20. template<>
  21. struct enum_traits< components > {
  22. static constexpr bool bit_index = true;
  23. };
  24.  
  25. template< typename t >
  26. struct flag_bits : std::bitset< static_cast< int >(t::count) > {
  27. flag_bits(t bit) // implicit
  28. { this->set( static_cast< int >( bit ) ); }
  29.  
  30. flag_bits(typename flag_bits::bitset set)
  31. : flag_bits::bitset(set) {}
  32. };
  33.  
  34. template< typename e >
  35. typename std::enable_if< enum_traits< e >::bit_index,
  36. flag_bits< e > >::type
  37. operator | (flag_bits< e > set, e next)
  38. {
  39. return set | flag_bits< e >(next);
  40. }
  41.  
  42. template< typename e >
  43. typename std::enable_if< enum_traits< e >::bit_index,
  44. flag_bits< e > >::type
  45. operator | (e first, e next)
  46. {
  47. return flag_bits< e >(first) | next;
  48. }
  49.  
  50. int main() {
  51. std::cout << (components::comp_01 | components::comp_50) << '\n';
  52. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
0000000000000000000010000000000000000000000000000000000000000000000001