tags:

views:

312

answers:

1

With GCC, I could do packing of enums using attribute((packed)), but it seems the closest thing in MSVC, #pragma pack, does not work on enums. Does anyone know of a way to pack enums into 1 byte instead of the usual integer size?

+2  A: 

This is MSVC specific:

// 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));

For reference see here: MSDN -> enum

I think that's C#. I've never seen such syntax in C++. If it works though, that's really cool. I'll refrain from downvoting because I'm unsure.
rmeador
maybe MSVC already implements c++ 0x, but then it should be "class enum" if I am not mistaken
lothar
It's a C++/CLI extension: http://msdn.microsoft.com/en-us/library/ms173702.aspx
Eclipse
No, it's not C++/CLI or C#. It's a MS extension to plain C++, similar to what gcc does with attribute((packed)).
From the link explaining the warning: "An extension to the language under /clr was used without /clr. You can disable C4480".It's a c++/cli extension that you can use in native c++.
Eclipse
@rmeador: read the answer -- it is MSVC specific. That means you won't find it in other compilers.
Kasprzol