470,833 Members | 1,582 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

portable code and enums of various sizes

I have some legacy C++ code which requires some enums to be 1 or 2
bytes in size.

I'd ideally like to be able to specify that a few carefully selected
enums are a particular size.

By default, g++ seems to create enums of 4 bytes in length, unless I
use the -fshort-enums option. I don't much want to use this all or
nothing approach in case it breaks library code etc, and there's no
guarantee that other compilers have a comparable option in any case.

I could of course replace the enums with unsigned char or unsigned
shorts, but I wonder if there's a more elegant C++ solution, Is there
any way I can create a class containing an unsigned char or unsigned
short, but with the semantics of an enum?

--
Simon Elliott http://www.ctsn.co.uk
Feb 23 '06 #1
2 2711

Simon Elliott wrote:
I have some legacy C++ code which requires some enums to be 1 or 2
bytes in size.

I'd ideally like to be able to specify that a few carefully selected
enums are a particular size.

By default, g++ seems to create enums of 4 bytes in length, unless I
use the -fshort-enums option. I don't much want to use this all or
nothing approach in case it breaks library code etc, and there's no
guarantee that other compilers have a comparable option in any case.

I could of course replace the enums with unsigned char or unsigned
shorts, but I wonder if there's a more elegant C++ solution, Is there
any way I can create a class containing an unsigned char or unsigned
short, but with the semantics of an enum?


Well you could use const char and const short variables in a header
file.
Alternatively keep them as enums (ensuring they fit into the values)
and in your class contain 1 or 2 byte variables to hold their numeric
equivalents. You can still switch on them using the enum names.

Feb 23 '06 #2
On 23/02/2006, Earl Purple wrote:
Well you could use const char and const short variables in a header
file.
Alternatively keep them as enums (ensuring they fit into the values)
and in your class contain 1 or 2 byte variables to hold their numeric
equivalents. You can still switch on them using the enum names.


Yes. I did the latter as its simple and allows us to keep the enum
constants which are useful for type safety in function calls.

--
Simon Elliott http://www.ctsn.co.uk
Feb 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by SpaceCowboy | last post: by
4 posts views Thread by David Rasmussen | last post: by
6 posts views Thread by Noah Roberts | last post: by
13 posts views Thread by James Harris | last post: by
17 posts views Thread by SW1 | last post: by
reply views Thread by **Developer** | last post: by
131 posts views Thread by pemo | last post: by
1 post views Thread by pistmaster | last post: by
5 posts views Thread by copx | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.