1

使用 GCC,我可以使用属性(( packed )) 对枚举进行打包,但似乎 MSVC 中最接近的东西 #pragma pack 不适用于枚举。有谁知道将枚举打包成 1 个字节而不是通常的整数大小的方法?

4

1 回答 1

3

这是特定于 MSVC 的:

// instances of this enum are packed into 1 unsigned char
// warning C4480: nonstandard extension used
enum foo : unsigned char { first, second, last }; 
assert(sizeof(foo) == sizeof(unsigned char));

// instances of this enum have the common size of 1 int
enum bar { alpha, beta, gamma };
assert(sizeof(bar) == sizeof(int));

有关参考,请参见此处: MSDN -> 枚举

于 2009-05-07T22:23:49.020 回答