public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Size of enum‏
@ 2012-02-05 16:29 Alexandre Almeida
  2012-02-05 20:40 ` David Brown
  0 siblings, 1 reply; 7+ messages in thread
From: Alexandre Almeida @ 2012-02-05 16:29 UTC (permalink / raw)
  To: gcc


What do you think about making enum types have only the size needed for the number of constants held?
If an enum type has 256 constants or less, for example, it needs only one byte. If it has between 257 and 65536 constants, in the other hand, it needs two bytes.
 		 	   		  

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Size of enum‏
  2012-02-05 16:29 Size of enum‏ Alexandre Almeida
@ 2012-02-05 20:40 ` David Brown
  2012-02-05 20:51   ` David Brown
  2012-02-05 21:13   ` Jonathan Wakely
  0 siblings, 2 replies; 7+ messages in thread
From: David Brown @ 2012-02-05 20:40 UTC (permalink / raw)
  To: Alexandre Almeida; +Cc: gcc

On 05/02/12 17:29, Alexandre Almeida wrote:
>
> What do you think about making enum types have only the size needed
> for the number of constants held? If an enum type has 256 constants
> or less, for example, it needs only one byte. If it has between 257
> and 65536 constants, in the other hand, it needs two bytes.
>

I think you have the wrong mailing list - this is for the development of 
the gcc compiler, not for development /using/ gcc.  You probably want 
gcc-help@gcc.gnu.org.

I'm sure someone will correct me if my details are wrong, but my 
understanding is this:

Enum types in C are all "int" - "signed int" by default, or "unsigned 
int" if one or more elements requires the range of unsigned int, or if 
one or more has an unsigned int suffix (e.g., "1u").

Enum types in C++ can be any integer type big enough to cover the 
required range.  I think most C++ compilers use the smallest integer 
type that covers the range.

gcc has a switch "-fshort-enums" that makes the compiler use the 
smallest usable integer type for its enums, which is the effect you are 
looking for.  Be careful when mixing modules (including libraries) with 
different settings for this flag - enum types will be incompatible 
between them.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Size of enum‏
  2012-02-05 20:40 ` David Brown
@ 2012-02-05 20:51   ` David Brown
  2012-02-05 21:13   ` Jonathan Wakely
  1 sibling, 0 replies; 7+ messages in thread
From: David Brown @ 2012-02-05 20:51 UTC (permalink / raw)
  To: gcc; +Cc: gcc

On 05/02/12 17:29, Alexandre Almeida wrote:
>
> What do you think about making enum types have only the size needed
> for the number of constants held? If an enum type has 256 constants
> or less, for example, it needs only one byte. If it has between 257
> and 65536 constants, in the other hand, it needs two bytes.
>

I think you have the wrong mailing list - this is for the development of 
the gcc compiler, not for development /using/ gcc.  You probably want 
gcc-help@gcc.gnu.org.

I'm sure someone will correct me if my details are wrong, but my 
understanding is this:

Enum types in C are all "int" - "signed int" by default, or "unsigned 
int" if one or more elements requires the range of unsigned int, or if 
one or more has an unsigned int suffix (e.g., "1u").

Enum types in C++ can be any integer type big enough to cover the 
required range.  I think most C++ compilers use the smallest integer 
type that covers the range.

gcc has a switch "-fshort-enums" that makes the compiler use the 
smallest usable integer type for its enums, which is the effect you are 
looking for.  Be careful when mixing modules (including libraries) with 
different settings for this flag - enum types will be incompatible 
between them.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Size of enum‏
  2012-02-05 20:40 ` David Brown
  2012-02-05 20:51   ` David Brown
@ 2012-02-05 21:13   ` Jonathan Wakely
  2012-02-05 22:40     ` James Dennett
  1 sibling, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2012-02-05 21:13 UTC (permalink / raw)
  To: David Brown; +Cc: Alexandre Almeida, gcc

2012/2/5 David Brown :
>
> Enum types in C++ can be any integer type big enough to cover the required
> range.  I think most C++ compilers use the smallest integer type that covers
> the range.

With the three C++ compilers I tried enums are int-sized for
compatiblity reasons, so that enums declared in a header file included
by C code and C++ code will agree on the type layout.

C++11 allows you to fix the underlying type of an enum, to use a specific size:

enum E : unsigned char { E1, E2 };

The syntax isn't valid in C, so there's no reason to use the same
layout as a C enum would.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Size of enum‏
  2012-02-05 21:13   ` Jonathan Wakely
@ 2012-02-05 22:40     ` James Dennett
  2012-02-06 22:15       ` Alexandre Almeida
  0 siblings, 1 reply; 7+ messages in thread
From: James Dennett @ 2012-02-05 22:40 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: David Brown, Alexandre Almeida, gcc

On Sun, Feb 5, 2012 at 12:58 PM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> 2012/2/5 David Brown :
>>
>> Enum types in C++ can be any integer type big enough to cover the required
>> range.  I think most C++ compilers use the smallest integer type that covers
>> the range.
>
> With the three C++ compilers I tried enums are int-sized for
> compatiblity reasons, so that enums declared in a header file included
> by C code and C++ code will agree on the type layout.

A decade ago I dealt with at least one platform where that was not the
case (meaning that using enums had to be disallowed in header files
that defined interfaces between C and C++ code).

I don't recall whether C allows enums types to vary in size, but in
practice I've not noticed a C compiler that did.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Size of enum‏
  2012-02-05 22:40     ` James Dennett
@ 2012-02-06 22:15       ` Alexandre Almeida
  2012-02-06 23:22         ` Andrew Pinski
  0 siblings, 1 reply; 7+ messages in thread
From: Alexandre Almeida @ 2012-02-06 22:15 UTC (permalink / raw)
  To: gcc


Okay, I am sorry for not knowing that there is an option to make enum types as short as possible. Anyway, I think it should be a default option. Do you agree?

 		 	   		  

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Size of enum‏
  2012-02-06 22:15       ` Alexandre Almeida
@ 2012-02-06 23:22         ` Andrew Pinski
  0 siblings, 0 replies; 7+ messages in thread
From: Andrew Pinski @ 2012-02-06 23:22 UTC (permalink / raw)
  To: Alexandre Almeida; +Cc: gcc

2012/2/6 Alexandre Almeida <meta.lx@hotmail.com>:
>
> Okay, I am sorry for not knowing that there is an option to make enum types as short as possible. Anyway, I think it should be a default option. Do you agree?

No I don't agree because that would cause an ABI change.  Note some
targets (arm-eabi though not arm-linux-eabi) already enable it by
default as it is required by the ABI.

Thanks,
Andrew Pinski

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-02-06 22:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-05 16:29 Size of enum‏ Alexandre Almeida
2012-02-05 20:40 ` David Brown
2012-02-05 20:51   ` David Brown
2012-02-05 21:13   ` Jonathan Wakely
2012-02-05 22:40     ` James Dennett
2012-02-06 22:15       ` Alexandre Almeida
2012-02-06 23:22         ` Andrew Pinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).