Eric said:
Michael said:
Keith said:
[...]
Note: typedef does not really create new types but convenient shortcuts.
New types are defined with struct.
Or union, or enum, or [], or *, or (). There are numerous ways to
create new types in C; struct is just the most flexible.
Hmmm, one can look at it like that. I guess I did not express myself
clear enough: I meant new as in "truly new".
For me, [], *, and () do not really create any _new_ type but sort of
perform the "usual derivations".
Enumerations are a convenient way to create identifiers for
constants, nothing more; [...]
The constant identifiers are not of any new type; they
are all `int'. The `enum foo' type itself, however, is
brand-new and did not exist in the language until the program
declared it.
You are right.
As mentioned above, I mainly use enumerations to obtain distinct
constant identifiers. Very seldom, I use the new type itself.
However, in conjunction with C99's designated initializers, I
at one time or another have wished for a way to run through
the "set" of values belonging to an enumeration type... Then, one
could really use the _type_.
[...] to be considered truly creating a _new_
type, IMO enums lack a way to run through all enumeration constants
and only through them even for non-contiguous enumerations.
It's true that enum types lack a lot of useful machinery.
However, the particular lack you mention is also shared by
struct types and floating-point types[*] (and arrays and unions
and pointers, but you've already said you don't consider them
"truly new"). And while it's possible to run through all the
values of an integer type, it's not exactly easy to do so while
remaining within the machinery of the type itself (that is,
without resorting to a known-to-be-wider type). This lack of
machinery for enum isn't unique among C's types.
Well, up to now I have never had the wish to iterate through
all struct members. However, in the case of
<
[email protected]> (compute the offset
of all structure members automatically) this may have been
convenient, so I won't dispute the possible usefulness...
As it is, I quite egocentrically just thought of the possible
and conceivable uses of all the formally new types (for me);
as arrays and pointers fall under "usual derived" types, I will
skip them.
enums basically would be much more useful if they came closer to
the notion of a set and provided some membership relation. As they
don't, they are IMO reduced to mere identifier providers where the
useful identifiers usually do not belong to objects of the type
in question...
With unions, one probably can decide one way or the other.
Seldomly, it _would_ be nice to be able to pick the ith incorporated
member, but this is no criterion against union types as "new" types.
As one could create aggregates similar to structs using arrays of
unions, I probably underestimated unions.
Thinking about it, everything probably comes down to the "flexibility"
Keith mentioned; unfortunately, I lack better words to distinguish
between my notion of "truly new" and formally new.
[*] C99 provides nextafter() and nexttoward() and their
variants, which could be used for this purpose. However, they
are not "in the language" in the same way `double' itself is;
note that they need not be present in freestanding environments.
Yep; wish they would have been there when I was into this kind
of stuff -- then more of my old code would be less ugly...
Cheers
Michael