How can I use an enum class in a boolean context?

后端 未结 7 1902
醉酒成梦
醉酒成梦 2021-01-07 15:59

I have some generic code that works with flags specified using C++11 enum class types. At one step, I\'d like to know if any of the bits in the flag are set. Cu

7条回答
  •  一生所求
    2021-01-07 16:41

    Like @RMatin says. But you could overload operator!

    bool operator!(E e) {
      return e == static_cast(0);
    }
    

    So that you can use the !!e idiom

    if(!!e) {
      ...
    }
    

提交回复
热议问题