使用MSVC ++编译器打包枚举

时间:2009-05-07 22:11:36

标签: c++ enums packing

使用GCC,我可以使用属性((压缩)来打包枚举,但它似乎是MSVC中最接近的东西,#pragma pack,不起作用在枚举上。有没有人知道如何将枚举打包成1个字节而不是通常的整数?

1 个答案:

答案 0 :(得分:2)

这是特定于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 -> enum