PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > enums ain't no good
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
enums ain't no good

Réponse
 
LinkBack Outils de la discussion
Vieux 27/11/2007, 04h24   #1
copx
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut enums ain't no good

C's enum type disappoints me a lot.

You cannot define which type of integer variable is used. This contradicts
C's low level spirit. "I want a number variable. I do not care about size or
execution speed." feels like typical ultra high level scripting language
design. The compiler could not optimize an enum if it wanted to, because you
cannot even specify whether memory requirements or speed are your primary
concern. Enums were meant to replace these endless lists of defines you find
all over older C source:
#define FOO 0
#define BAR 1
...
It is a technique commonly used to give index numbers meaningful names.
However, the variables which hold such numbers often do not need to be wider
than one byte and are used a lot. In one program I had many enum-type
variables as parts of my data structures and the amount of wasted RAM was
excessive when I used C enums, because the compiler (GCC) always used
(unsigned) ints, even if the entire range of valid values fitted into a
single byte. IMHO the definition of an enum should allow you to specify the
integer type. For example:

typedef int8_t enum {
FOO,
BAR
} MY_ENUM;

Not giving the programmer this type of control makes me feel like I am using
another language. What was the reasoning behind that decision? Did the guy
who designed the enum type also suggest to replace all of C's integer types
with "number"?

copx


  Réponse avec citation
Vieux 27/11/2007, 04h38   #2
Leo Havmøller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: enums ain't no good

> C's enum type disappoints me a lot.
> You cannot define which type of integer variable is used.


Yes, a common annoyance. However, most compilers for embedded software has a
"treat enums like ints" option, that can be disabled, in order to achieve
auto-sizing enum's.

Leo Havmøller.

  Réponse avec citation
Vieux 27/11/2007, 04h41   #3
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: enums ain't no good

copx wrote:

Ain't no good is a double negative....

> C's enum type disappoints me a lot.
>

As they should, they are more than a little broken.

<snip>

>
> Not giving the programmer this type of control makes me feel like I am using
> another language. What was the reasoning behind that decision? Did the guy
> who designed the enum type also suggest to replace all of C's integer types
> with "number"?
>

Where size matters (typically but not exclusively on embedded systems),
many compilers provide extensions to size enums.

--
Ian Collins.
  Réponse avec citation
Vieux 27/11/2007, 05h49   #4
Ben Pfaff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: enums ain't no good

"copx" <copx@gazeta.pl> writes:

> C's enum type disappoints me a lot.
>
> You cannot define which type of integer variable is used. This contradicts
> C's low level spirit.


There's not much difference between, on one hand, defining an
enum type without a tag and then typedef'ing an integer type to
what name you like and, on the other hand, defining an enum type
with a tag and using the enum type directly. If the latter falls
short, you can just use the former.
--
Go not to Usenet for counsel, for they will say both no and yes.
  Réponse avec citation
Vieux 27/11/2007, 07h54   #5
copx
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: enums ain't no good


"Leo Havmøller" <rtxleh@nospam.nospam> schrieb im Newsbeitrag
news:474b9f52$0$2100$edfadb0f@dtext02.news.tele.dk ...
>> C's enum type disappoints me a lot.
>> You cannot define which type of integer variable is used.

>
> Yes, a common annoyance. However, most compilers for embedded software has
> a "treat enums like ints" option, that can be disabled, in order to
> achieve auto-sizing enum's.


Yes, IIRC GCC has such an option, too. It chooses the narrowest type
possible. However, that is not the same as having full control over which
type is used, and as I have said, I think this should be doable at syntax
level.




  Réponse avec citation
Vieux 27/11/2007, 08h04   #6
copx
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: enums ain't no good


"Ben Pfaff" <blp@cs.stanford.edu> schrieb im Newsbeitrag
news:87myt0nsp8.fsf@blp.benpfaff.org...
> "copx" <copx@gazeta.pl> writes:
>
>> C's enum type disappoints me a lot.
>>
>> You cannot define which type of integer variable is used. This
>> contradicts
>> C's low level spirit.

>
> There's not much difference between, on one hand, defining an
> enum type without a tag and then typedef'ing an integer type to
> what name you like and, on the other hand, defining an enum type
> with a tag and using the enum type directly. If the latter falls
> short, you can just use the former.


I have read about that technique in some embedded programming journal
recently. Yes, it works, but it is an ugly workaround for what is a language
design flaw IMO. Enums are supposed to define the type which is used to
store the enum values after all.


  Réponse avec citation
Vieux 27/11/2007, 13h24   #7
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: enums ain't no good

copx wrote:
> C's enum type disappoints me a lot.
> [...]
> It is a technique commonly used to give index numbers meaningful names.
> However, the variables which hold such numbers often do not need to be wider
> than one byte and are used a lot. In one program I had many enum-type
> variables as parts of my data structures and the amount of wasted RAM was
> excessive when I used C enums, because the compiler (GCC) always used
> (unsigned) ints, even if the entire range of valid values fitted into a
> single byte.


You may have missed the fact that you can use the enum
declaration just to define the constants, and then store their
values in any type you are fond of:

enum { FOO, BAR, BAZ };
char foo = FOO;
unsigned long long bar = BAR;
long double baz = BAZ;

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 30/11/2007, 23h16   #8
Mark L Pappin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: enums ain't no good

"copx" <copx@gazeta.pl> writes:
> Enums are supposed to define the type which is used to
> store the enum values after all.


Chapter and Verse?

mlp
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h54.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18099 seconds with 16 queries