fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Default_t{} Default;
  5. struct Toggle_t{} Toggle;
  6.  
  7. struct FlagSet{
  8. uint m_set;
  9. uint m_reset;
  10.  
  11. constexpr FlagSet operator|(const FlagSet other) const{
  12. return {
  13. ~m_reset & other.m_set & ~other.m_reset |
  14. ~m_set & other.m_set & other.m_reset |
  15. m_set & ~other.m_set,
  16. m_reset & ~other.m_reset |
  17. ~m_set & ~other.m_set & other.m_reset|
  18. ~m_reset & other.m_set & other.m_reset};
  19. }
  20.  
  21. constexpr FlagSet& operator|=(const FlagSet other){
  22. *this = *this|other;
  23. return *this;
  24. }
  25. };
  26.  
  27. struct Flag{
  28. const uint m_bit;
  29.  
  30. constexpr FlagSet operator= (bool val) const{
  31. return {(uint)val<<m_bit,(!(uint)val)<<m_bit};
  32. }
  33.  
  34. constexpr FlagSet operator= (Default_t) const{
  35. return {0u,0u};
  36. }
  37.  
  38. constexpr FlagSet operator= (Toggle_t) const {
  39. return {1u<<m_bit,1u<<m_bit};
  40. }
  41.  
  42. constexpr uint operator& (FlagSet i) const{
  43. return i.m_set & (1u<<m_bit);
  44. }
  45.  
  46. constexpr operator FlagSet() const{
  47. return {1u<<m_bit,0u}; //= set
  48. }
  49.  
  50. constexpr FlagSet operator|(const Flag other) const{
  51. return (FlagSet)*this|(FlagSet)other;
  52. }
  53. constexpr FlagSet operator|(const FlagSet other) const{
  54. return (FlagSet)*this|other;
  55. }
  56. };
  57.  
  58. constexpr uint operator& (FlagSet i, Flag f){
  59. return f & i;
  60. }
  61.  
  62.  
  63. constexpr Flag Flag1{0};
  64. constexpr Flag Flag2{1};
  65. constexpr Flag Flag3{2};
  66.  
  67. constexpr auto NoFlag1 = (Flag1=false);
  68.  
  69.  
  70. void foo(FlagSet f={0,0}){
  71. f |= Flag1|Flag2;
  72. cout << ((f & Flag1)?"1":"0");
  73. cout << ((f & Flag2)?"2":"0");
  74. cout << ((f & Flag3)?"3":"0");
  75. cout << endl;
  76. }
  77.  
  78. int main() {
  79.  
  80. foo();
  81. foo(Flag3);
  82. foo(Flag3|(Flag2=false));
  83. foo(Flag3|NoFlag1);
  84. foo((Flag1=Toggle)|(Flag2=Toggle)|(Flag3=Toggle));
  85.  
  86. return 0;
  87. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
120
123
103
023
003