Why is the enum incompatible with bit fields in Windows?

前端 未结 1 1595
粉色の甜心
粉色の甜心 2021-01-24 19:58

I\'m working on EBDS protocol interface for Windows and Linux. I\'m trying to pack all the data required by the protocol into structs, then I write the struct itself and all the

相关标签:
1条回答
  • 2021-01-24 20:17

    C++ standard does not specify how bit fields are exactly laid out. Many compilers will use regular integers for each bit field components, which means faster processing but larger structs, unless you specify a different preference which you have done in case of Linux.

    See here for the algorithm VS2010 uses.

    Edit: There is a problem with your code. Remember that with a signed base type, one bit of the bit field will be consumed by the sign bit. And your enums (like most people's) may be signed (whether they are, is implementation defined) and thus you may see surprises when you store E_EXTENDED_COMMANDS in mMessageType and immediately find out that the value is not there.

    With recent compilers, you can force the enums to be unsigned and avoid this problem.

    0 讨论(0)
提交回复
热议问题