|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
> 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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"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 |
|
![]() |
| Outils de la discussion | |
|
|