枚举标志的使用位集的优缺点是什么?
namespace Flag {
enum State {
Read = 1 << 0,
Write = 1 << 1,
Binary = 1 << 2,
};
}
namespace Plain {
enum State {
Read,
Write,
Binary,
Count
};
}
int main()
{
{
unsigned int state = Flag::Read | Flag::Binary;
std::cout << state << std::endl;
state |= Flag::Write;
state &= ~(Flag::Read | Flag::Binary);
std::cout << state << std::endl;
} {
std::bitset<Plain::Count> state;
state.set(Plain::Read);
state.set(Plain::Binary);
std::cout << state.to_ulong() << std::endl;
state.flip();
std::cout << state.to_ulong() << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我可以看到,bitsets具有更方便的set/clear/flip函数来处理,但枚举标记的使用是一种更广泛的方法.
在我的日常代码中,我应该使用什么是bitset的可能缺点?
无论std::bitset与C风格的enum有管理的重要标志缺点。首先,让我们考虑以下示例代码:
namespace Flag {
enum State {
Read = 1 << 0,
Write = 1 << 1,
Binary = 1 << 2,
};
}
namespace Plain {
enum State {
Read,
Write,
Binary,
Count
};
}
void f(int);
void g(int);
void g(Flag::State);
void h(std::bitset<sizeof(Flag::State)>);
namespace system1 {
Flag::State getFlags();
}
namespace system2 {
Plain::State getFlags();
}
int main()
{
f(Flag::Read); // Flag::Read is implicitly converted to `int`, losing type safety
f(Plain::Read); // Plain::Read is also implicitly converted to `int`
auto state = Flag::Read | Flag::Write; // type is not `Flag::State` as one could expect, it is `int` instead
g(state); // This function calls the `int` overload rather than the `Flag::State` overload
auto system1State = system1::getFlags();
auto system2State = system2::getFlags();
if (system1State == system2State) {} // Compiles properly, but semantics are broken, `Flag::State`
std::bitset<sizeof(Flag::State)> flagSet; // Notice that the type of bitset only indicates the amount of bits, there's no type safety here either
std::bitset<sizeof(Plain::State)> plainSet;
// f(flagSet); bitset doesn't implicitly convert to `int`, so this wouldn't compile which is slightly better than c-style `enum`
flagSet.set(Flag::Read); // No type safety, which means that bitset
flagSet.reset(Plain::Read); // is willing to accept values from any enumeration
h(flagSet); // Both kinds of sets can be
h(plainSet); // passed to the same function
}
Run Code Online (Sandbox Code Playgroud)
即使您可能认为这些问题很容易在简单的示例中发现,但最终它们会在每个在c样式enum和上构建标志的代码库中蔓延std::bitset。
那么,如何做才能更好地保护类型呢?首先,C ++ 11的范围枚举是对类型安全性的改进。但这极大地阻碍了便利。解决方案的一部分是对范围限定的枚举使用模板生成的按位运算符。这是一篇很棒的博客文章,解释了它如何工作并提供了工作代码:https : //www.justsoftwaresolutions.co.uk/cplusplus/using-enum-classes-as-bitfields.html
现在,让我们看看它是什么样的:
enum class FlagState {
Read = 1 << 0,
Write = 1 << 1,
Binary = 1 << 2,
};
template<>
struct enable_bitmask_operators<FlagState>{
static const bool enable=true;
};
enum class PlainState {
Read,
Write,
Binary,
Count
};
void f(int);
void g(int);
void g(FlagState);
FlagState h();
namespace system1 {
FlagState getFlags();
}
namespace system2 {
PlainState getFlags();
}
int main()
{
f(FlagState::Read); // Compile error, FlagState is not an `int`
f(PlainState::Read); // Compile error, PlainState is not an `int`
auto state = Flag::Read | Flag::Write; // type is `FlagState` as one could expect
g(state); // This function calls the `FlagState` overload
auto system1State = system1::getFlags();
auto system2State = system2::getFlags();
if (system1State == system2State) {} // Compile error, there is no `operator==(FlagState, PlainState)`
auto someFlag = h();
if (someFlag == FlagState::Read) {} // This compiles fine, but this is another type of recurring bug
}
Run Code Online (Sandbox Code Playgroud)
此示例的最后一行显示了一个在编译时仍然无法捕获的问题。在某些情况下,比较平等可能是真正需要的。但是大多数时候,真正的意思是if ((someFlag & FlagState::Read) == FlagState::Read)。
为了解决这个问题,我们必须区分枚举器的类型和位掩码的类型。这是一篇详细介绍我之前提到的部分解决方案的改进的文章:https : //dalzhim.github.io/2017/08/11/Improving-the-enum-class-bitmask/ 免责声明:我是此后的文章。
使用上一篇文章中由模板生成的按位运算符时,您将获得我们在上一段代码中演示的所有好处,同时还捕获了该mask == enumerator错误。
你编译时开启了优化吗?不太可能存在 24 倍速度系数。
对我来说,bitset 更优越,因为它为你管理空间:
int/long long版本中的空间。unsigned char/ unsigned short- 但我不确定实现是否应用此优化)