如何解决如何查询用作位掩码的枚举类的有效性而不转换为基础类型
我有一个用于位掩码的枚举类,就像这样(在虚幻引擎中,因此是 uint8 类型)
enum class Level : uint8
{
None = 0x0,Debug = 0x1,Info = 0x2,Warning = 0x4,...
}
我为 |
、&
和 ^
添加了内联运算符,以便我可以使用成员作为实际位掩码(例如 mask = Level::Warning | Level::Debug
),如下所示:
inline Level operator&(const Level& lhs,const Level& rhs)
{
return (Level)((uint8)lhs & (uint8)rhs);
}
现在在某个时候我想查询,是否设置了一个位。但是,如果不强制转换为 uint8
,这似乎不起作用:
if(mask & Level::Debug) //does not work,C2451
if((uint8)(mask & Level::Debug)) //does work
编辑:
目标是使调用代码尽可能短(和可读)。在这种情况下 &
的含义似乎与使用诸如额外的 any
方法 as suggested in a similar question 之类的调用一样清晰。添加 == Level::Debug
代码当然也有效,但不会缩短使用的代码。更改运算符的返回类型有效,但基本上将问题转移到 Level myLevel = Level::Debug | Level::Warning
之类的赋值上,因此我不会整体改进我的代码 imo。
解决方法
您需要 operator bool - 但 operator bool 只能为类类型定义。所以 - 定义类 - 并将您的枚举放入其中,如下所示:
class Level
{
public:
enum Value : std::uint8_t
{
None = 0x0,Debug = 0x1,Info = 0x2,Warning = 0x4,Error = 0x8
};
constexpr Level(Value value) noexcept : value(value) {}
friend constexpr bool operator == (Level lhs,Level rhs)
{ return lhs.value == rhs.value; }
friend constexpr bool operator != (Level lhs,Level rhs)
{ return lhs.value != rhs.value; }
friend constexpr Level operator & (Value lhs,Value rhs)
{
return Value(static_cast<uint8_t>(lhs) & static_cast<uint8_t>(rhs));
}
friend constexpr Level operator & (Level lhs,Level rhs)
{
return lhs.value & rhs.value;
}
constexpr explicit operator bool() const noexcept { return value != Value::None; }
private:
Value value;
};
工作demo。
像这样使用:
int main() {
Level l = Level::None;
if (l & Level::Info) return -1;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。