ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You can use a compiler option -fshort-enums to define the default size of enums, but can you do it on a per enum basis? The following from the draft standard I have would suggest you can.
Quote:
7.2 Enumeration declarations
...
5 Each enumeration defines a type that is different from all other types. Following the closing brace of an enum-specifier,
each enumerator has the type of its enumeration. Prior to the closing brace, the type of each enumerator is the type
of its initializing value. If an initializer is specified for an enumerator, the initializing value has the same type as the
expression. If no initializer is specified for the first enumerator, the type is an unspecified integral type. Otherwise the
type is the same as the type of the initializing value of the preceding enumerator unless the incremented value is not
representable in that type, in which case the type is an unspecified integral type sufficient to contain the incremented
value.
6 The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration.
If no integral type can represent all the enumerator values, the enumeration is ill-formed. It is implementationdefined
which integral type is used as the underlying type for an enumeration except that the underlying type shall not
be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list
is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value of sizeof()
applied to an enumeration type, an object of enumeration type, or an enumerator, is the value of sizeof() applied to
the underlying type.
An online version which shows this in context is here.
Visual Studio allows
enum F00 : char {_0,_1,_2};
yet g++ complains about being used before defined, instead the following will compile but will give a sizeof of 4 bytes.
enum FOO{_0 =char(0),_1,_2};
An enum is just syntactic sugar for a typedef and a list of constants.
Not quite since an enum will restrict the numbers that are allowed to be assigned into a variable defined as an enum. Because you can only use the labels to assign into the enum type (and if I remember correctly increment and decrement operators)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.