Why is the enum incompatible with bit fields in Windows?

孤街浪徒 提交于 2019-12-02 04:08:52

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!