mercredi 19 juin 2019

Equality operator for QFlags with scoped enum

I have some flags defined as follows, using a scoped enum:

enum class Capability : int
{
  NoCapabilities  = 0,
  SomethingCool = 1,
  UberCool = 1 << 1,
  EvenCooler = 1 << 2,
};
Q_DECLARE_FLAGS( Capabilities, Capability )

Now, I am trying to use the equality operator:

Capabilities(DataCapability::NoCapabilities) == Capability::NoCapabilities

I cannot use testFlag in the current example, since NoCapabilities = 0. Tihs works if the enum is not scoped (removing class keyword).

Apparently, casting to int (static_cast<int>(NoCapabilities)) works, but it's really ugly.

What is the approach to solve this? Is it a bad practice to have a 0 zero value and test it?

Aucun commentaire:

Enregistrer un commentaire