473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2860

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
4
by: David Rasmussen | last post by:
The problem comes up in all sorts of contexts. But here is an example: We want to model the basics of chess, maybe for making a chess program, or maybe just to make an interactive board or ... ...
6
by: Noah Roberts | last post by:
I am having an interesting compilation error that makes me wonder if enums can conflict with each other. For example: class A { enum X { TEST, TEST2 }; public: ....
13
by: James Harris | last post by:
Hi, Can someone recommend a book that will teach me how to approach C programming so that code is modularised, will compile for different environments (such as variations of Unix and Windows),...
17
by: SW1 | last post by:
I wrote a small program which does something like tftp - transfering files and some chat, anyway i got a problem with fwrite, here is a snippet of my code: while(length > 0) { putchar('.');...
0
by: **Developer** | last post by:
I know about Shell.ExtractIconEx But suppose I have a .ICO file that has 3 or 4 icons of various sizes in it. I'd like to extract then, determine the sizes and display them. How can I do...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
1
by: pistmaster | last post by:
Hi, I have a client server application which has various messages containing enums, ie I serialize classes of the form: class MyClass { MyEnum e; }
5
by: copx | last post by:
How portable are direct bit operations? Which ones are portable? I have never bothered learning such low-level stuff (I have an excuse: I am not a professional programmer), so I really don't know....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.