现代C ++中声明位掩码类型的现代方法是什么?对此有推荐吗?
// old C, not recommended in C++
#define COLOR_BLUE 1 << 0
#define COLOR_RED 1 << 1
// Prefer enum class instead of enum, also it's a good practice to use unsigned, not enums(int)
enum Color
{
blue = 1 << 0;
red = 1 << 1;
}
// a class with access only to these members
// additional lines to delete unnecessary data, delete ctors, etc
class Color
{
public:
static constexpr unsigned int blue = 1 << 0;
static constexpr unsigned int red = 1 << 1;
Color() = delete;
}
bitset.set(Color::blue);