LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2006, 05:16 PM   #1
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
C++ defining the underlying type for an enum.


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};

Does anybody know if this can be done?

Last edited by dmail; 10-11-2006 at 05:17 PM.
 
Old 10-11-2006, 09:27 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
An enum is just syntactic sugar for a typedef and a list of constants. E.g.,

Code:
typedef enum {
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
} day_t;
is like

Code:
typedef int day_t;

const day_t SUNDAY = 0, MONDAY = 1, TUESDAY = 2, WEDNESDAY = 3,
            THURSDAY = 4, FRIDAY = 5, SATURDAY = 6;
So, you can just write code like the above, except use "short" or "char" instead of "int".
 
Old 10-11-2006, 09:39 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Dan04
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)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions about enum. What would be a good use of enum? RHLinuxGUY Programming 5 06-11-2006 01:13 PM
enum inheritance in C++? graemef Programming 2 01-27-2006 10:14 PM
HOTSWAP: where to find #ENUM signal? iflorea Programming 0 11-20-2005 06:58 AM
C Enum exvor Programming 3 09-27-2005 02:51 PM
C, enum confusion cigarstub Programming 2 09-27-2005 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration