mardi 30 novembre 2021

enum class with scoping but without having to cast to underlying type

I have this:

enum class Categories : uint32_t {
    C6 = 0x00000020,
    C7 = 0x00000040,
    C8 = 0x00000080,
    ...
};

I chose an enum class because it is great for scoping. But the downside is that when I need to use the categories as mask bits for bitwise operations I need to cast them to uint32_t first.

Example:

uint32_t masterCat = ((uint32_t)MyClass::Categories::C6) | ((uint32_t)MyClass::Categories::C7);

Is there a way I can get the same scoping benefit, without having to cast before using each time ? If I use a regular enum, then I lose the scoping benefit :(

Example:

uint32_t masterCat = (MyClass::Categories::C6) | (MyClass::Categories::C7);

Aucun commentaire:

Enregistrer un commentaire